AASM4.1.1 can't work on Rails4

291 views Asked by At

I have a model that contain a field called "status".I have change it into String type in DB.And I also use enum in AASM.
here is my code:

class Card < ActiveRecord::Base
  include AASM

  enum status: {
    default: 0,
    published: 1,
    deleted: 2
  }

  aasm column: :status, enum: true, skip_validation_on_save: true, no_direct_assignment: true do
    state :default, initial: true
    state :published
    state :deleted

    event :publish do
      transitions from: :default, to: :published
    end

    event :delete do
      transitions from: :published, to: :deleted
    end
  end
end

And I apply it like this:

Card card = Card.first
card.publish

No matter I call card.publish or card.delete,I will get an error like this:

NoMethodError: undefined method `name' for nil:NilClass
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/core/state.rb:15:in `=='
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `block in state_object_for_name'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `each'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `find'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `state_object_for_name'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/aasm.rb:62:in `aasm_fire_event'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/persistence/active_record_persistence.rb:175:in `block in aasm_fire_event'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/persistence/active_record_persistence.rb:175:in `aasm_fire_event'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/base.rb:81:in `block in event'
    from (irb):13
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'2.0.0-p598 :014 >

It puzzle me for a few days.Any help will be appreciate.

1

There are 1 answers

2
alto On

Shouldn't it be

enum status: [
  :default,
  :published,
  :deleted
]

? If that doesn't help, please open open an issue on GitHub and I will take a closer look at it.