我正在使用Ruby on Rails 3.2.9而我只是尝试使用state_machine gem。我有以下声明:
class Article < ActiveRecord::Base
state_machine :attribute => :status, :initial => :unconfirmed do
state :confirmed, :value => 'confirmed'
state :unconfirmed, :value => 'unconfirmed'
event :confirm do
transition :unconfirmed => :confirmed
end
event :unconfirm do
transition :confirmed => :unconfirmed
end
end
end
我还有confirmed_at
模型的Article
属性,旨在保留:status
切换到:confirmed
时的最后一次。
如何将confirmed_at
属性初始化为当前时间?每次confirmed_at
事件被触发时应我如何设置:confirm
当前时间?
答案 0 :(得分:0)
看起来这就是before_transition
的用途。
before_transition :confirmed => any - :confirmed, :do => :set_confirmed_at
然后按该名称添加方法:
def set_confirmed_at
self.confirmed_at = Time.now
end