pluginaweek/state_machine — Issue #144: Initial state cannot be a string?
Author: nathanl
Created: 2011-12-02 20:07:19+00:00
State: open

I just stumbled on a behavior in state_machine that confuses me and I wondered if you could give some clarification.

I had the following code on an ActiveRecord model:

```ruby
state_machine :status, :initial => 'requested' do

event :approve do
transition :requested => :approved
end

# more events here...
end
```

When I called `approve!`, I got `StateMachine::InvalidTransition: Cannot transition status via :approve from "requested" (Reason(s): Status cannot transition via "approve")`

While puzzling over this, I idly changed `:initial => 'requested'` to `initial => :requested` and was surprised that it started working.

I was calling this transition on a record that had been loaded from the database. Whether I had specified the initial status of the original object as a symbol or string, it was saved in the database as a string, and the new ActiveRecord object certainly pulled a string value out of the database. I don't see how it would "know" whether `status` on the original ActiveRecord object had been initialized with a symbol or string.

Is this behavior by design or is it a bug? I'm not even sure how I would do this if I wanted to. :)