# Acts\_as\_state\_machine error

**URL:** <https://www.ruby-forum.com/t/acts-as-state-machine-error/145301>\
**Category:** Rails\
**Created:** [August 7, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-error/145301 "2008-08-07T18:44:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 7, 2008, 6:44pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-error/145301/1 "2008-08-07T18:44:23Z")

</div>

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!’

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 8, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/acts-as-state-machine-error/145301/2 "2008-08-08T22:57:04Z")

</div>

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 🙂

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.

---

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 10, 2008, 9:04am UTC](https://www.ruby-forum.com/t/acts-as-state-machine-error/145301/3 "2008-08-10T09:04:59Z")

</div>

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!

On Aug 8, 10:51 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
