发生变化时触发父类的观察者
我正在编写一个时间线,当对象(例如“产品”)发生更改时,它将事件存储在表中。
我已经为 Product 编写了一个观察者,它使用“after_create”和“before_update”等组成合理的日志消息和关联数据。这非常有效。
现在,每个产品都有很多变体,而且每个变体都有很多价格。然而,它们通常以相同的形式进行编辑。但只有产品类的更改才会导致观察者触发“before_update”。
我希望对变体进行更改以触发关联产品的产品观察器,并以同样的方式导致对价格的更改触发与价格相关的变体关联的产品。
这与 Rails 2.3.3+ 中的 :touch 功能没有什么不同。我也尝试过依赖该函数,但触摸似乎没有触发 before_update 观察者方法。
你会怎么做?
I'm writing a timeline, that stores events in a table when there are changes to objects, such as "products".
I've written an observer for Product that composes a sensible log message and associated data using "after_create" and "before_update" and so on. This works perfectly.
Now, each Product has many Variants, and also, each Variant has many Prices. However they are typically edited in the same form. But only changes to the Product-class cause the observer to trigger "before_update".
I would like changes to Variants to trigger the Product-observer for the associated product, and in the same way cause changes to Prices trigger the product associated with the Variant associated with the Price.
This is not unlike the :touch functionality in Rails 2.3.3+. I've also tried to depend on that function, but the touching does not seem to trigger the before_update observer method.
How would you do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据我目前的经验,触摸应该触发常规回调,而不是观察者回调。因为这些略有不同。
Based on my current experience touch should trigger regular callbacks but not observer callbacks. As these are slightly different.