Prev | Current Page 159 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

Still, we need
a definition to build from, so here??™s a plain English explanation of callbacks:
Active Record objects can perform a large number of methods or actions throughout
their existence; callbacks allow you to insert your business logic before or after many of
these actions.
59
C H A P T E R 4
?–  ?–  ?– 
As we??™ve mentioned previously, some plain English examples of tasks accomplished by
callbacks may also help you to visualize the concept:
??? Before you save an update to the Account table, set the last updated field to the current
time.
??? Before you delete a record from the Account table, make sure you delete records from
the contact table that have the deleted record??™s account_id.
??? After you find a promotional code record, calculate the current subscription cost.
As these examples show, callbacks are really just a nice and simple way to add some potentially
complex business logic right into the process of communicating with your database. From
a developer??™s point of view, you can implement callbacks by either manually overwriting the callback
method or by using a callback macro.


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