开发者

AASM callbacks not working

I'm using AASM to manage states and just wanted to have some columns that kept track of the times the state were changed, but the callbacks don't seem to be working. The problems, of course, could be with m开发者_如何学JAVAy methods, I'm just not sure.

        aasm_state :active, :after => :activate
        aasm_state :inactive
        aasm_state :deactivated, :after => :deactivate

        aasm_event :active do
          transitions :to => :active, :from => [:inactive]
          transitions :to => :active, :from => [:deactivated]
        end

        aasm_event :deactivated do
          transitions :to => :deactivated, :from => [:active]
        end

       def activate
         activated_at = Time.now
      end  

      def deactivate
        deactivated_at = Time.now
      end


In Rails 3 version of AASM, you need to apply callbacks to transition events, rather than on state. So you code can be -

    aasm_state :active

    aasm_event :active, :after => :activate do 

        transitions :to => :active, :from => [:inactive]
        transitions :to => :active, :from => [:deactivated]
    end


when using aasm, you dont just call your activate / deactivate function on your model.

you also need to save the model, so when you do object.activate, after that also do object.save (at least that is how it was in the last version i used)

transitions seem to be written ok, so i dont think those are the problem

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜