Prev | Current Page 168 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

Again, the following example shows the callback we just
defined in action:
example = Account.new(:Account_Name => "Kevin")
example.save
puts Account.Account_lastsaved
# Sun Oct 15 21:41:39 Eastern Standard Time 2006
before_create
This method is executed before an ActiveRecord::Base.save statement when the object does
not already have a corresponding record in the database:
class Account < ActiveRecord::Base
def before_create
self.Account_updated = Time.now
end
end
This example populates the Account_updated attribute with the time immediately before
an Account object is created. The following code shows an example use of this callback:
Example = Account.new(:Account_Name => "Kevin")
puts Account_updated # Sun Oct 15 21:41:39 Eastern Standard Time 2006
after_create
This method is executed after an ActiveRecord::Base.save statement when the object does
not have a corresponding record already in the database.
class Account < ActiveRecord::Base
def after_create
logger.info( "Account was created at #{Time.now}" )
end
end
This example assumes that we have a logger object to which we are recording certain
actions.


Pages:
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180