Prev | Current Page 162 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


As you probably guessed, in most cases, callback macros are the way to go. Because of the
inheritance hierarchy, callback macros probably operate like you would intend in most cases,
and they give you the most options for implementation. Actually, four types of callbacks are
accepted by callback macros: method references, callback objects, inline methods, and inline
eval methods (though inline eval methods are now deprecated).
Probably the easiest, and most common, approach you will see is the method reference.
You simply define a protected or private method in your model:
class Account < ActiveRecord::Base
before_save :setupdate
private
def setupdate
self.Account_Update = Time.now
end
end
Sometimes, you might need to perform a large number of tasks with a callback that you
don??™t want to clutter up you model with or perhaps share some functionality for callbacks
CHAPTER 4 ?–  CORE FEATURES OF ACTIVE RECORD 61
across multiple models. These situations are a good times to use objects. There are a couple of
steps required to use this approach to creating callbacks:
1.


Pages:
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174