pluginaweek/state_machine — Issue #130: Weird issue with method missing
Author: jmazzi
Created: 2011-10-18 17:28:25+00:00
State: open

If you setup a model to use a column other than 'state' and use the following code it raises an error. I came across the issue while playing with the state_machine API:

```ruby
class Service < ActiveRecord::Base
state_machine :status, :initial => :pending do
event :active do
transition :pending => :active
end

event :pending do
transition any => :pending
end
end
end
```

```
irb(main):001:0> Service.state_machine.state
=> []
irb(main):002:0> Service.first.pending!
Service Load (0.1ms) SELECT "services".* FROM "services" LIMIT 1
NoMethodError: undefined method `state' for #
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/attribute_methods.rb:60:in `method_missing'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/validator.rb:151:in `block in validate'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `each'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `validate'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:302:in `_callback_before_19'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:404:in `_run_validate_callbacks'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/validations.rb:212:in `run_validations!'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:391:in `block in _run_validation_callbacks'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:207:in `block in _conditional_callback_around_18'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/integrations/active_model.rb:466:in `block in around_validation'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition_collection.rb:149:in `block in run_actions'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition_collection.rb:169:in `catch_exceptions'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition_collection.rb:147:in `run_actions'
... 39 levels...
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition_collection.rb:147:in `run_actions'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition_collection.rb:60:in `perform'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/transition.rb:209:in `perform'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/event.rb:158:in `fire'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/event.rb:218:in `block in add_actions'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/machine.rb:704:in `call'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/machine.rb:704:in `block (2 levels) in define_helper'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/event.rb:223:in `block in add_actions'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/machine.rb:704:in `call'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/state_machine-1.0.2/lib/state_machine/machine.rb:704:in `block (2 levels) in define_helper'
from (irb):2
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /Users/justin/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `'
from script/rails:6:in `require'
from script/rails:6:in `

'irb(main):003:0>
```

I put a test project up at https://github.com/jmazzi/state_machine_bug if you wanna try it out. Probably not a real issue but certainly an odd one.