# acts\_as\_state\_machine error

**URL:** <https://discuss.rubyonrails.org/t/acts-as-state-machine-error/32957>\
**Category:** rubyonrails-talk\
**Created:** [August 7, 2008, 4:43pm UTC](https://discuss.rubyonrails.org/t/acts-as-state-machine-error/32957 "2008-08-07T16:43:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil6](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@phil6](https://discuss.rubyonrails.org/u/phil6)\
**Post date:** [August 7, 2008, 4:43pm UTC](https://discuss.rubyonrails.org/t/acts-as-state-machine-error/32957/1 "2008-08-07T16:43:43Z")

</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 &nbsp;&nbsp;from vendor/plugins/acts\_as\_state\_machine/lib/ acts\_as\_state\_machine.rb:151:in `current_state' &nbsp;&nbsp;from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:92:in `next\_states' &nbsp;&nbsp;from vendor/plugins/acts\_as\_state\_machine/lib/ acts\_as\_state\_machine.rb:92:in `select' &nbsp;&nbsp;from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:92:in `next\_states' &nbsp;&nbsp;from vendor/plugins/acts\_as\_state\_machine/lib/ acts\_as\_state\_machine.rb:96:in `fire' &nbsp;&nbsp;from vendor/plugins/acts_as_state_machine/lib/ acts_as_state_machine.rb:206:in `accept!'

---

<div class="post-metadata">

**Author:** ![ascherkus\_rails](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@ascherkus\_rails](https://discuss.rubyonrails.org/u/ascherkus_rails)\
**Post date:** [August 8, 2008, 8:51pm UTC](https://discuss.rubyonrails.org/t/acts-as-state-machine-error/32957/2 "2008-08-08T20:51:52Z")

</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:** ![phil6](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@phil6](https://discuss.rubyonrails.org/u/phil6)\
**Post date:** [August 10, 2008, 7:04am UTC](https://discuss.rubyonrails.org/t/acts-as-state-machine-error/32957/3 "2008-08-10T07:04:20Z")

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