• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
ActiveRecord Callbacks - RORLab Season 3-5
 

ActiveRecord Callbacks - RORLab Season 3-5

on

  • 395 views

 

Statistics

Views

Total Views
395
Views on SlideShare
294
Embed Views
101

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 101

http://rorlab.org 101

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

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

    ActiveRecord Callbacks - RORLab Season 3-5 ActiveRecord Callbacks - RORLab Season 3-5 Presentation Transcript

    • Active RecordCallbacksRor lab. season 3- the 5th round -April 27th, 2013ChangHoon Jeong(@seapy)13년 4월 27일 토요일
    • Contents• The Object Life Cycle• Validations Overview• Validation Helpers• CommonValidationOptions• ConditionalValidation• CustomValidations• Working withValidationErrors• DisplayingValidation Errorsin theView• Callbacks Overview• Available Callbacks• Running Callbacks• Skipping Callbacks• Halting Execution• Relational Callbacks• Conditional Callbacks• Callback Classes• Observers• Transaction CallbacksValidations Callbacks13년 4월 27일 토요일
    • Life-cycle ofObjectsDB13년 4월 27일 토요일
    • Life-cycle ofObjectsActiveRecordModelDB13년 4월 27일 토요일
    • Life-cycle ofObjectsObjectCreate SaveUpdateDeleteActiveRecordModelDB13년 4월 27일 토요일
    • Life-cycle ofObjectsObjectCreate SaveUpdateDeleteValidationloadActiveRecordModelDB13년 4월 27일 토요일
    • Callbacks• Methods : protected or private• Before or after certain moments of anobject’s life cycle• 6 events: create, save, update, delete, validate, load• Register using a macro-style class method★ as an ordinary method or★ supply as a block13년 4월 27일 토요일
    • As OrdinaryMethodsclass 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  endendMacro-style class methods :13년 4월 27일 토요일
    • A Blockclass User < ActiveRecord::Base  validates :login, :email, :presence => true   before_create do |user|    user.name = user.login.capitalizeif user.name.blank?  endendMacro-style class methods :13년 4월 27일 토요일
    • Available Callbacks★Creating an Object• before_validation• after_validation• before_save• around_save• before_create• around_create• after_create• after_save★Updating an Object• before_validation• after_validation• before_save• around_save• before_update• around_update• after_update• after_save★Destroying an Object• before_destroy• around_destroy• after_destroySaveDestroyCreateUpdateValidationDB13년 4월 27일 토요일
    • 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>13년 4월 27일 토요일
    • MethodsTriggering Callbacks• create• create!• decrement!• destroy• destroy_all• increment!• save• save!• save(:validate =>false)• toggle!• update• update_attribute• update_attributes• update_attributes!• valid?‘after_find’ callback• all• first• find• find_all_by_attribute• find_by_attribute• find_by_attribute!• last13년 4월 27일 토요일
    • MethodsSkipping Callbacks• decrement• decrement_counter• delete• delete_all• find_by_sql• increment• increment_counter• toggle• touch• update_column• update_all• update_counters13년 4월 27일 토요일
    • 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_sqltouch13년 4월 27일 토요일
    • 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 > ROLLBACK13년 4월 27일 토요일
    • RelationalCallbacks13년 4월 27일 토요일
    • ConditionalCallbacks• As with validations★ Using :if and :unless with a Symbol★ Using :if and :unless with a String★ Using :if and :unless with a Proc13년 4월 27일 토요일
    • ConditionalCallbacksas a symbolas a stringas a Proc* multiple conditions possible!13년 4월 27일 토요일
    • 감사합니다.
    •   13년 4월 27일 토요일
    • public/protected/privateclasspublicprotectedprivateclasspublicprotectedprivateparentchildprivate_methodhttp://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails(self.)protected_method13년 4월 27일 토요일