Prev | Current Page 280 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

column :model, :string
CHAPTER 5 ?–  BONUS FEATURES 122
t.column :column, :string
t.column :record_id, :integer
t.column :changed_from, :string
t.column :changed_to, :string
t.column :created_on, :datetime
end
Since we??™ll be leaving such a trail of Audit objects (every change to a Cow object, for example,
can create up to three Audit objects), we should have a way of getting relevant ones. We
can use the SingletonMethods module for this. Here is a method that can query the Audit
model for us, in a nice, neat package:
module SingletonMethods
def revisions_for object
id = object.is_a?(ActiveRecord::Base) ? object.id : object
Audit.find(:all, :conditions => ["class_name = ? AND record_id = ?",
self.name, id])
end
end
We can pass in the id of the record we wish to query or the object itself, and we will receive
an array of Audit objects. Because the methods in the SingletonMethods module are (not surprisingly)
singleton methods, they are applied only to the class we originally called audits on.
Therefore, we should call the revisions_for method through that class.


Pages:
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292