I am using Ruby on Rails 3.2.9 and I am just trying to use the state_machine gem. I have following statements:
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
I have also a confirmed_at attribute for the Article model intended to keep the last time when the :status switches to :confirmed.
How to initialize the confirmed_at attribute to the current time? How should I set the confirmed_at to the current time each time the :confirm event is triggered?
Looks like that’s what
before_transitionis for.Then add a method by that name: