activerecord callbacks - rorlab season 3-5
TRANSCRIPT
Active Record Callbacks
Ror lab. season 3- the 5th round -
April 27th, 2013
ChangHoon Jeong(@seapy)
13년 4월 27일 토요일
Contents
• The Object Life Cycle
• Validations Overview
• Validation Helpers
• Common Validation Options
• Conditional Validation
• Custom Validations
• Working with Validation Errors
• Displaying Validation Errors in the View
• Callbacks Overview
• Available Callbacks
• Running Callbacks
• Skipping Callbacks
• Halting Execution
• Relational Callbacks
• Conditional Callbacks
• Callback Classes
• Observers
• Transaction Callbacks
Validations Callbacks
13년 4월 27일 토요일
Life-cycle of Objects
DB
13년 4월 27일 토요일
Life-cycle of Objects
ActiveRecord
Model
DB
13년 4월 27일 토요일
Life-cycle of Objects
Object
Create Save
UpdateDelete
ActiveRecord
Model
DB
13년 4월 27일 토요일
Life-cycle of Objects
Object
Create Save
UpdateDelete Validat
ion
load
ActiveRecord
Model
DB
13년 4월 27일 토요일
Callbacks
• Methods : protected or private
• Before or after certain moments of an object’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 block
13년 4월 27일 토요일
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 endend
Macro-style class methods :
13년 4월 27일 토요일
A Block
class User < ActiveRecord::Base validates :login, :email, :presence => true before_create do |user| user.name = user.login.capitalize if user.name.blank? endend
Macro-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_destroy
Save
Destroy
Create
UpdateValid
atio
n DB
13년 4월 27일 토요일
After_find> After_initialize
class 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일 토요일
Methods Triggering 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!• last
13년 4월 27일 토요일
Methods Skipping Callbacks
• decrement• decrement_counter• delete• delete_all• find_by_sql• increment• increment_counter• toggle• touch• update_column• update_all• update_counters
13년 4월 27일 토요일
Methods Calling Callbacks Methods Skipping Callbacks
create
create!
decrement! decrement / ~_counter
destroy delete
destroy_all delete_all
increment! increment / ~_counter
save
save!
save(false)
toggle! toggle
update
update_attribute update_column
update_attributes update_all
update_attributes! update_counters
valid?
find_by_sql
touch
13년 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 > ROLLBACK
13년 4월 27일 토요일
Relational Callbacks
13년 4월 27일 토요일
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
13년 4월 27일 토요일
Conditional Callbacks
as a symbol
as a string
as a Proc
* multiple conditions possible!
13년 4월 27일 토요일
감사합니다.����������� ������������������
13년 4월 27일 토요일
public/protected/private
class
publicprotected
private
class
public
protected
private
parent
childprivate_method
http://stackoverflow.com/questions/4495078/protected-and-private-methods-in-rails(self.)protected_method
13년 4월 27일 토요일