Matt Elhotiby
Matt Elhotiby

Reputation: 44066

send back a variable with before_save

Ok i am trying to create a before_save that checks to see that fields change and return them in a variable like this

if @user.update_attributes(params[:user]) 
  send_email_update(@user)
  logger.info @changed

In the model i have a before save

 before_save :check_changes

 def check_changes
   @changed = self.changed
 end

Is there a way to do this in any way...basically sending back a variable with the changed fields or if there is a suggestion on how to tackle something like this

Upvotes: 0

Views: 264

Answers (1)

Christopher Manning
Christopher Manning

Reputation: 4557

Use ActiveModel::Dirty.previous_changes()

params[:user] = { 'name' => 'Bob' }
if @user.update_attributes(params[:user]) 
  send_email_update(@user)
  logger.info @user.previous_changes  # => { 'name' => ['Bill', 'Bob'] }

Upvotes: 1

Related Questions