如何访问 datamapper 观察者中的旧状态值?
我正在使用 dm-observer 来观察我的 dm 模型,并且我需要根据模型内的状态更改执行一些操作。我已经发现 @state 用于存储更新的状态值,但我无法弄清楚如何访问旧的状态值。在下面的示例中,我使用了“old_state”,但显然这不起作用。
class Adam
include DataMapper::Resource
property :id, Serial
property :name, String
property :state, Integer
end
class AdamObserver
include DataMapper::Observer
observe Adam
before :update do
if old_state == 1 && @state == 2
#do something
end
end
end
I'm using dm-observer to observe my dm models, and I need to perform some actions based on state changes within the model. I've figured out that @state is used to store the updated state value, but I have not been able to figure out how to access the old state value. In the example below I've used "old_state", but obviously that does not work.
class Adam
include DataMapper::Resource
property :id, Serial
property :name, String
property :state, Integer
end
class AdamObserver
include DataMapper::Observer
observe Adam
before :update do
if old_state == 1 && @state == 2
#do something
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 #original_attributes 哈希访问原始值,该哈希由属性对象索引。所以代码可能看起来像这样:
You can access original values via #original_attributes hash which is indexed by property objects. So the code could look like that: