acts_as_state_machine error

If I try and change the state of an unsaved object I get the following error. If I first save the object then do the call it is fine... is this a known bug or am I doing something wrong? (most likely the latter!)

pt.accept!

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.to_sym   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:151:in `current_state'   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:92:in `next_states'   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:92:in `select'   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:92:in `next_states'   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:96:in `fire'   from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:206:in `accept!'

acts_as_state_machine's initial state is only set when saving your object during a before_save callback

If you call a state transition when your current state is nil (i.e., a new object) then you get the exact error you pasted :slight_smile:

Usually for new objects you DON'T set the state at all and let acts_as_state_machine use its :initial option to save it for you.

Actually, what I discovered was that in my migration to add the state (in my case status) column to the table I did not have a default value set. When I added that the object got one too. A bit of a gotcha, actually. Should be in the readme!