开发者

How to access old state values within datamapper observer?

开发者 https://www.devze.com 2023-01-07 11:18 出处:网络
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'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
    observ开发者_StackOverflowe Adam

    before :update do
        if old_state == 1 && @state == 2
            #do something 
        end
    end
end 


You can access original values via #original_attributes hash which is indexed by property objects. So the code could look like that:

if original_attributes[properties[:state]] == 1 && state == 2
  # do something
end
0

精彩评论

暂无评论...
验证码 换一张
取 消