DirtyAttributes accept BigDecimal types as mutated types

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

, I have this:

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

      

It creates the quantity as BigDecimal and accepts the object_id as part of the changes.

Does anyone know how to avoid this behavior?

+3


source to share


1 answer


If in after_save you need to check if some BigDecimal field is actually changed, you need to reload the generated rails 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

      

That it compares before ( amount_change[0]

) and after ( amount_change[1]

) values ​​in float form.



So, in the after_save callback, you can:

after_save :do_something_if_amount_changed

def do_something_if_amount_changed
  if amount_changed?
    do_something
  end
end

      

0


source







All Articles