Copyright © tutorialspoint.com

Ruby on Rails - Callback Functions

previous

During the life cycle of an active record object, you can hook into 8 events:

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.

previous

Copyright © tutorialspoint.com