The state_machine gem really is a great piece of machinery, it works flawlessly.
I have one question though I couldn't easily find an answer to. Is is possible to perform a before_transition validation for the initial state?
My use case is this: I have an initial state of 'started' in a state machine for an Assignment model. Users can have many assignments. However, any given user can only have one assignment in the started state. I can't figure out how to accomplish this using State Machine. I've now solved it using an ActiveRecord callback, but I would rather have everthing controlled by State Machine.
I would appreciate any help.
The relevant code:
class Assignment < ActiveRecord::Base
belongs_to :user
validate :single_started, :on => :create
state_machine :state, :initial_state => :started do
# events omitted...
end
def single_started
errors.add :base, "Only one started assignment allowed" unless user.allowed_assignment?
end
end