Option 4: Create a ClientHistory
model that is identical to Client
with the addition of a client_id
column that references the appropriate Client
. Then, a before_save
callback could do something like this:
before_save :track_history
def track_history
return if(new_record? || !changed?)
old = Hash[attribute_names.map { |name| [name == 'id' ? 'client_id' : name, self.send("#{name}_was")] }]
ClientHistory.create!(old)
end
The new_record? || !changed?
could be pushed to an :if
option on the before_save
call of course. You might want to filter the attribute_names
to leave more things out of course and you might want to only track things that have changed, both of these are pretty trivial modifications (but be careful about how you track to-nil
and from-nil
transitions).