Prev | Current Page 165 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

find
operation.
?– Note To use the after_find method, you must define it as a method in your ActiveRecord class; there
is no callback macro for this specific method. The lack of a callback macro for the after_find method is built
into the design of Active Record to help improve performance when you use after_find. The method is
called once for every record that the find method returns, so executing the method via a macro would be
very expensive for your processing resources.
The following example adds a fullname attribute to the account records that are found.
Because we are adding an attribute that is not directly derived from the database table, we
must use the class attribute accessor method (cattr_accessor) as well. We discuss the details
of cattr_accessor in other chapters.
class Account < ActiveRecord::Base
cattr_accessor :Account_fullname
def after_find
self.Account_fullname = self.Account_firstname + " " + self.Account_lastname
end
end
With this example, the following code would trigger the after_find method, so each record
within the results would now have an Account_fullname attribute, even though your database
table does not have that specific field or collection of data:
useraccount = Account.


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