anti-k anti-k - 2 months ago 12
Ruby Question

tracking changes in attributes

I do have a problem with tracking changes in attributes
here is my model

class Ticket < ActiveRecord::Base
include Generator
around_update :create_history
def create_history
@event_status = "Status to #{self.status}.\n" if status_changed?
@event_owner = "Now it is provided by #{self.stuff.name}\n" if stuff_id_changed?
yield
histories.create(event: "Changed! #{@event_status} #{@event_owner} On #{self.updated_at.utc}") if @event_status || @event_owner
end
end


action update is trivial

def update
flash[:notice] = "Your ticket successfully updated."
@ticket.update!(ticket_params.merge(status: 0))
end


But that does not work(specs on creating history fails. Im fighting with this for hours. Would be very grateful for any response pointing my mistake

Answer

It is difficult to say what is exactly wrong with your code.

One thing is ticket_params.merge(status: 0). Since that your status is always 0 and never changed.

As long as I can't see ticket_params I can't say exactly what is wrong with stuff_id changes.

I created the same around_update callback in my test project and it works just fine.

The main thing I would like to notice that you should use paper_trail gem for dealing with your models history. The gem is awesome and you would really love it.