• Like
ActiveRecord Callbacks & Observers, Season 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

ActiveRecord Callbacks & Observers, Season 2

  • 920 views
Published

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

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

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
920
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
11
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \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

Transcript

  • 1. Ror lab. season 2 - the 7th round - Active RecordCallbacks & Observers October 6th, 2012 Hyoseong Choi
  • 2. 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
  • 3. Life-cycle of Objects DB
  • 4. Life-cycle of Objects DB Active Record el od M
  • 5. Life-cycle ofActiveRecord el Objects od M DB Create Save Object Delete Update
  • 6. Life-cycle ofActiveRecord el Objects od M DB Create Save Validation Object Delete Update load
  • 7. 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
  • 8. 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
  • 9. 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>
  • 10. 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?
  • 11. Methods Skipping Callbacks• decrement• decrement_counter• delete• delete_all• find_by_sql• increment• increment_counter• toggle• touch• update_column• update_all• update_counters
  • 12. 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
  • 13. 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
  • 14. RelationalCallbacks
  • 15. 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
  • 16. Conditional Callbacks as a symbol as a string as a Proc* multiple conditions possible!
  • 17. Callback Classes • To reuse callback methods for other models as an instance method a model objectinstancemethod
  • 18. Callback Classesas a class method
  • 19. 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
  • 20. Creating Observers
  • 21. Registering Observers • config/application.rb • or config/environments
  • 22. Sharing Observers as a model name
  • 23. Transaction Callbacks• after_commit or after_rollback
  • 24. 감사합니다.
  • 25.   
  • 26. 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
  • 27. 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
  • 28. 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