Home > Software engineering >  Update a column on update of others columns properly
Update a column on update of others columns properly

Time:03-07

I would like know a properly way to change a column value if others columns change, i know the classic way with one case return self.prices_updated_at = Time.zone.now if currency_changed?

But i dont know how write it more properly than that when i have many cases

return unless currency_changed? || price1_changed? || price2_changed? ||
                  price3_changed? || price4_changed? ||
                  price5_changed? || price6_changed?

    self.prices_updated_at = Time.zone.now

CodePudding user response:

You can use changed_attributes

return unless changed_attributes.keys & %w[currency price1 price2 price3 price4 price5 price6]
self.prices_updated_at = Time.zone.now
  • Related