Learning Ruby on Rails
Ruby on Rails Quick Guide
Ruby Tutorial
Ruby on Rails Resources
Selected Reading
© 2011 TutorialsPoint.COM
|
Ruby on Rails - Callback Functions
During the life cycle of an active record object, you can hook into 8 events:
- (-) save
- (-) valid?
- (1) before_validation
- (2) before_validation_on_create
- (-) validate
- (-) validate_on_create
- (3) after_validation
- (4) after_validation_on_create
- (5) before_save
- (6) before_create
- (-) create
- (7) after_create
- (8) after_save
Examples
class Subscription < ActiveRecord::Base
before_create :record_signup
private
def record_signup
self.signed_up_on = Date.today
end
end
class Firm < ActiveRecord::Base
# Destroys the associated clients and
# people when the firm is destroyed
before_destroy{
|record|Person.destroy_all "firm_id= #{record.id}"
}
before_destroy{
|record|Client.destroy_all "client_of= #{record.id}"
}
end
|
Check the following link for more detail on Callback Functions.
|
|
|