Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Rails 4 compatibility #111

Closed
troessner opened this issue Dec 15, 2013 · 2 comments
Closed

Add Rails 4 compatibility #111

troessner opened this issue Dec 15, 2013 · 2 comments

Comments

@troessner
Copy link
Owner

Coming from this issue: #108


Another way to repro this issue:

Gemfile:

source 'https://rubygems.org'
gem 'rails', '4.0.1'
gem 'pg'
gem "transitions", :require => ["transitions", "active_model/transitions"]

Migration:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :status
    end
  end
end

Model:

class User < ActiveRecord::Base
 include ActiveModel::Transitions
 state_machine :attribute_name => :status, :initial => :new do
  state :registered
 end
end

Console:

➜  app1  rails c
Loading development environment (Rails 4.0.1)
irb(main):001:0> u = User.new
NoMethodError: undefined method `call_action' for nil:NilClass
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/transitions-0.1.11/lib/active_model/transitions.rb:93:in `set_initial_state'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:375:in `_run__1719794837482552169__initialize__callbacks'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activesupport-4.0.1/lib/active_support/callbacks.rb:80:in `run_callbacks'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/core.rb:188:in `initialize'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/inheritance.rb:27:in `new'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/inheritance.rb:27:in `new'
    from (irb):1
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/valentin/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Error goes away when I set the initial state implicitly in a block.

@troessner
Copy link
Owner Author

@Valve I investigated this issue and this is NOT a rails 4 problem. The problem is that you your initial state name is not listed as state at all, hence the error.

If your model looks like that:

class User < ActiveRecord::Base
 include ActiveModel::Transitions
 state_machine :attribute_name => :status, :initial => :new do
  state :new
  state :registered
 end
end

everything works out fine, regardless of postgresql or rails version.
I have updated the documentation to make this more clearer: cc5a8ad

Actually the best thing to do would be to check during initialization of "transitions" if the initial state does exist: #112

-> Closing

@Valve
Copy link

Valve commented Dec 23, 2013

Thanks, @troessner

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants