• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
ActiveRecord Callbacks & Observers, Season 2
 

ActiveRecord Callbacks & Observers, Season 2

on

  • 1,141 views

This is a Keynote slide about the "ActiveRecord Callbacks & Observers"

This is a Keynote slide about the "ActiveRecord Callbacks & Observers"

Statistics

Views

Total Views
1,141
Views on SlideShare
840
Embed Views
301

Actions

Likes
0
Downloads
8
Comments
0

5 Embeds 301

http://rorlab.org 206
http://lab.wordstory.net 79
http://www.rorlab.org 12
http://211.60.184.138 3
http://www.hanrss.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

ActiveRecord Callbacks & Observers, Season 2 ActiveRecord Callbacks & Observers, Season 2 Presentation Transcript

  • Ror lab. season 2 - the 7th round - Active RecordCallbacks & Observers October 6th, 2012 Hyoseong Choi
  • Contents Validations Callbacks• The Object Life Cycle • Callbacks Overview• Validations Overview • Available Callbacks• Validation Helpers • Running Callbacks• Common Validation • Skipping Callbacks Options • Halting Execution• Conditional Validation • Relational Callbacks• Custom Validations • Conditional Callbacks• Working with Validation • Callback Classes Errors • Observers• Displaying Validation Errors • Transaction Callbacks in the View
  • Life-cycle of Objects DB
  • Life-cycle of Objects DB Active Record el od M
  • Life-cycle ofActiveRecord el Objects od M DB Create Save Object Delete Update
  • Life-cycle ofActiveRecord el Objects od M DB Create Save Validation Object Delete Update load
  • Callbacks• Methods : protected or private• Before or after certain moments of an object’s life cycle• 6create, save, update, delete, validate, load : events• Register using a macro-style class method ★ as an ordinary method or ★ supply as a block
  • Available Callbacks★ Creating an Object ★ Updating an Object ★ Destroying an Object• before_validation • before_validation • before_destroy• after_validation • after_validation • around_destroy• before_save • before_save • after_destroy• before_create • before_update• around_create • around_update• after_create • after_update• after_save • after_save DB Validation Create Save Update Destroy
  • After_find > After_initializeclass User < ActiveRecord::Base  after_initialize do |user|    puts "You have initialized an object!"  end   after_find do |user|    puts "You have found an object!"  endend >> User.newYou have initialized an object!=> #<User id: nil> >> User.firstYou have found an object!You have initialized an object!=> #<User id: 1>
  • MethodsTriggering Callbacks•• create create! ‘after_find’ callback• decrement! • all• destroy • first• destroy_all • find• increment! • find_all_by_attribute• save • find_by_attribute• save! • find_by_attribute!• save(:validate =>false)• toggle! • last• update• update_attribute• update_attributes• update_attributes!• valid?
  • Methods Skipping Callbacks• decrement• decrement_counter• delete• delete_all• find_by_sql• increment• increment_counter• toggle• touch• update_column• update_all• update_counters
  • Methods Calling Callbacks Methods Skipping Callbackscreatecreate!decrement! decrement / ~_counterdestroy deletedestroy_all delete_allincrement! increment / ~_countersavesave!save(false)toggle! toggleupdateupdate_attribute update_columnupdate_attributes update_allupdate_attributes! update_countersvalid? find_by_sql touch
  • Halting Execution• Queue ★ validations for model ★ registered callbacks ★ database operations• Wrapped in a Transaction ★ before_ callbacks : false or exception > stopped > ROLLBACK ★ after_ callbacks : exception > stopped > ROLLBACK
  • RelationalCallbacks
  • Conditional Callbacks• As with validations ★ Using :if and :unless with a Symbol ★ Using :if and :unless with a String ★ Using :if and :unless with a Proc
  • Conditional Callbacks as a symbol as a string as a Proc* multiple conditions possible!
  • Callback Classes • To reuse callback methods for other models as an instance method a model objectinstancemethod
  • Callback Classesas a class method
  • Observers• Similar to callbacks• for the callbacks with the code that isn’t directly related the model• without changing the code of the model• ex. sending confirmation email after user registration
  • Creating Observers
  • Registering Observers • config/application.rb • or config/environments
  • Sharing Observers as a model name
  • Transaction Callbacks• after_commit or after_rollback
  • 감사합니다.
  •   
  • Macro-style class methods : As Ordinary Methods class User < ActiveRecord::Base   validates :login, :email, :presence => true     before_validation :ensure_login_has_a_value     protected   def ensure_login_has_a_value     if login.nil?       self.login = email unless email.blank?     end   end
  • public/protected/private parent pr class iv at child e_ m private et ho class protected public d private (sel f.)p protected rote public ed_ ct met hodhttp://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails
  • Macro-style class methods : A Block class User < ActiveRecord::Base   validates :login, :email, :presence => true     before_create do |user|     user.name = user.login.capitalize if user.name.blank?   end