Reputation: 35189
I am using Aaron Pfeifer's state_machine gem in my Rails3 app -- it's (still) nifty.
I have a event whose outcome isn't known until it has been processed, and I would like to set the following state based on the outcome. The 'obvious' approach shown here doesn't work: it raises an ArgumentError: :picked_many is not a known state value
error.
What's the right way to set the state based on the outcome of an event? (Or am I just thinking about this wrong?)
class MyModel < ActiveRecord::Base
state_machine :initial => :top do
event :pickanumber do
transition any => any
end
state :picked_zero
state :picked_one
state :picked_many
end
def pickanumber(n)
self.state = case n
when 0 then :picked_zero
when 1 then :picked_one
else :picked_many
end
super
end
end
and a sample run:
> m = MyModel.create
=> #<MyModel id: 26, state: "top", ...>
> m.pickanumber(2)
ArgumentError: :picked_many is not a known state value
Upvotes: 1
Views: 1130
Reputation: 13583
State is internally stored as a string and not symbol.
This should work...
def pickanumber(n)
self.state = case n
when 0 then "picked_zero"
when 1 then "picked_one"
else "picked_many"
end
super
end
Upvotes: 1