3

I'm in a model callback (after_save) and one of the attributes is BigDecimal type. So when I change another attribute and check dirty attributes with changes method I have this:

{"amount"=>[#<BigDecimal:7f86aa3ac900,'-0.4E3',9(18)>, #<BigDecimal:7f86aa3ac838,'-0.4E3',9(18)>], "description"=>["vvvv", "ccc"]}

It instantiates amount as BigDecimal and takes object_id as part of the changes.

Has anyone an idea of how to avoid this behaviour?

AlexLarra
  • 841
  • 5
  • 18

1 Answers1

0

If in after_save you need to check if a particular BigDecimal field is really changed, you need to reload rails-created method attr_name_changed? (in your case amount_changed?):

 def amount_changed?
   if amount_change.present?
     amount_change[0].to_f != amount_change[1].to_f
   end
 end

What it does it compares before (amount_change[0]) and after (amount_change[1]) values in float form.

So then in after_save callback you can do:

after_save :do_something_if_amount_changed

def do_something_if_amount_changed
  if amount_changed?
    do_something
  end
end
Yury Kaspiarovich
  • 2,027
  • 2
  • 19
  • 25