Overriding ActiveRecord's find method





1
Date Submitted Wed. Oct. 11th, 2006 3:08 PM
Revision 1 of 1
Helper jeremec
Tags activerecord | find | override | rails | Ruby
Comments 0 comments
From time to time you may have reasons to override the default find method that ActiveRecord provides. In this example, we'll store a side-copy of the record attributes so that we have some basis for discovering changes to the data.

note: this will not override dynamic finders such as find_by_id

class User < ActiveRecord::Base
  attr_accessor :original_values

  def self.find(*options)
    results = super(*options)
    results.each do |result|
      result.original_values = result.attributes
    end
    return results
  end
end
 

Jereme Claussen

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Scripter ctiggerf
Syntax Master dannyboy

Votes Down


Syntax Master sundaramkumar