Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to know what exactly changed using an observer in ruby on rails?

I need send an email alert when the price of a product changes. Is it possible do this with ActiveRecord::Observer or do I need use programming logic in the edit form?

like image 835
Erik Escobedo Avatar asked Nov 28 '25 19:11

Erik Escobedo


1 Answers

You can use Dirty Objects in order to achieve that, in this specific case, you'll have something like this in your observer.

FooMailer.deliver_alert(foo) if foo.price_changed?

According to: "Once you save a dirty object it clears out its changed state tracking and is once again considered unchanged" You should ask for the prince_changed? before the save operation is done.

Hope It helps you.

like image 192
jpemberthy Avatar answered Nov 30 '25 11:11

jpemberthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!