slindsey3000
slindsey3000

Reputation: 4301

Pass a hash to ActiveRecord#save

I have a an after_save callback on Scorecard class.

In this callback I create and save an object in a different model BalanceLedger.

I need to pass a message to this object being created in the after_save of Scorecard

Is this possible?

e.g. (where this is not valid but trying to get my point across)

scorecard.total = new_total
scorecard.save(message: 'This is an Admin correction')

class Scorecard < ActiveRecord::Base

  after_save do
    BalanceLedger.create!(total: total, message: params[:message])
  end

Upvotes: 0

Views: 247

Answers (1)

ttrmw
ttrmw

Reputation: 161

Maybe use :attr_accessor to set a virtual attribute? You could set a message value, and retrieve it from within the callback.

scorecard.total = new_total
scorecard.message = 'This is an Admin correction'
scorecard.save

class Scorecard < ActiveRecord::Base    
  attr_accessor :message 

  after_save do
    BalanceLedger.create!(total: total, message: message)
  end
end

Upvotes: 1

Related Questions