简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM