The reason for this post is to get feedback as to whether there’s a “Rails Way” of doing what I’m attempting. The way I’m doing it seems a bit clunky and prone to human error.
I have multiple tables which I want to store history of including the time and date a record is updated. Who updates it is not important.
At the moment, I have put together the following [not yet tested, some syntax is probably wrong], but it seems like other people would have this need and that there would be a better way of doing it, whether there was a way of implementing this at the database level instead or some other inbuilt method/callback I’m not aware of.
The model CreateDdiPlanHistorical was generated including updated_at:datetime on top of the other fields seen below in the create_history method, my understanding is that should automatically populate on any records in the historicals table being added or modified.
(validations and associations stripped out for clarity)
class CallDdiPlan < ActiveRecord::Base
attr_accessible :active, :ddi_count, :plan_name, :price
after_save :create_history
private
def create_history
CallDdiPlanHistorical.create({:active => this.active, :ddi_count => this.ddi_count, :plan_name => this.plan_name, :price => this.price, :call_ddi_plan_id => this.id})
end
end
You could do some sort of roll-your-own solution, but if you really want to make the boss happy, use the audited gem: