How can I tell what fields have been updated?

I’ve scoured the Wiki and gone through the documentation with a fine
toothed comb, but I can’t seem to find this answer. I want to keep a
history of the changes made to a record, but can’t seem to find out what
fields have changed. I’m sitting in the update function, as you can see
below… I want to be able to get a list of what has changed so I can
add some pretty text to the history field to tell my users what has
changed.

def update
@bug = Bug.find(params[:id])
if @bug.update_attributes(params[:bug])
*** Here I should be able to edit my history field by telling what
has changed ***
flash[:notice] = ‘Bug was successfully updated.’
redirect_to :action => ‘show’, :id => @bug
else
render :action => ‘edit’
end
end

In the above example, I have the attributes before it has changed, and
the attributes after it has changed, but I’d rather not do a line by
line comparison. Does RoR have something that will automatically tell me
that the following fields have been modified? Then I can do a simple
comparison myself, rather than going through line by line.

Thanks.

On Monday, June 19, 2006, at 8:53 PM, phillip wrote:

if @bug.update_attributes(params[:bug])
the attributes after it has changed, but I’d rather not do a line by
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

@old = Bug.find(params[:id]).attributes
@new = params[:bug]
@new.select {|key,value| @old[key] != value}

returns an array of [key, value] for the changed items and what they
were changed to.

_Kevin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs