aasm неопределенный метод `состояние '
Я настраиваю свою модель rails 5.1.4 для поддержки состояний через aasm, у меня проблема с ней, по-видимому, метод не инициализируется state
для модели. Который, кажется, используется, когда я звоню item.save
так ломает мою модель. Другие методы работают нормально.
Я настроил (и запустил) свою миграцию так:
add_column :item, :aasm_state, :string
и настроил его в файле модели item.rb:
class Item
include AASM
[...]
aasm do
state :unpublished, initial: true
state :published
state :blocked
event :publish do
transitions from: :unpublished, to: :published
end
event :unpublish do
transitions from: :published, to: :unpublished
end
event :block do
transitions from: :published, to: :blocked
end
event :unblock do
transitions from: :blocked, to: :published
end
end
[...]
end
в консоли рельсов я печатаю:
> item = Item.new
=> #<Item id: nil, title: nil, description: nil, created_at: nil, updated_at: nil, aasm_state: "unpublished">
> item.aasm_statenike
=> "unpublished"
> item.aasm.states.map(&:name)
=> [:unpublished, :published, :blocked]
> item.state #<===== WHERE IS STATE?
NoMethodError: undefined method `state' for #<Item:0x007f8d43263810>
from (irb):2
> item.save
NoMethodError: undefined method `state' for #<Item:0x007f8d4a30f550>
Did you mean? state=
from (irb):3
> item.publish
=> true
> item.aasm_state
=> "published"
> item.state #<===== WHERE IS STATE?
NoMethodError: undefined method `state' for #<Item:0x007f8d43263810>
from (irb):9
ОБНОВЛЕНИЕ добавление полной трассировки стека:
> begin
* item.save!
* rescue NoMethodError => e
* puts e.backtrace
* end
(1.0ms) BEGIN
(0.7ms) ROLLBACK
/Users/user/.rvm/gems/ruby-2.4.1/gems/activemodel-5.1.4/lib/active_model/attribute_methods.rb:432:in `method_missing'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/persistence/base.rb:36:in `aasm_read_state'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/instance_base.rb:12:in `current_state'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/persistence/active_record_persistence.rb:157:in `aasm_invalid_state?'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/persistence/active_record_persistence.rb:149:in `block in aasm_validate_states'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/persistence/active_record_persistence.rb:147:in `each'
/Users/user/.rvm/gems/ruby-2.4.1/gems/aasm-4.12.3/lib/aasm/persistence/active_record_persistence.rb:147:in `aasm_validate_states'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:413:in `block in make_lambda'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:600:in `catch'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:600:in `block in default_terminator'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:198:in `block in halting'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `block in invoke_before'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `each'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:507:in `invoke_before'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:130:in `run_callbacks'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activemodel-5.1.4/lib/active_model/validations.rb:405:in `run_validations!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activemodel-5.1.4/lib/active_model/validations/callbacks.rb:110:in `block in run_validations!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:131:in `run_callbacks'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activemodel-5.1.4/lib/active_model/validations/callbacks.rb:110:in `run_validations!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activemodel-5.1.4/lib/active_model/validations.rb:335:in `valid?'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/validations.rb:65:in `valid?'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/validations.rb:82:in `perform_validations'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/validations.rb:50:in `save!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/attribute_methods/dirty.rb:43:in `save!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:313:in `block in save!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/Users/user/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:210:in `transaction'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/transactions.rb:313:in `save!'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.4/lib/active_record/suppressor.rb:46:in `save!'
(pry):4:in `<main>'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:77:in `block in repl'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:67:in `loop'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:67:in `repl'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:38:in `block in start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/input_lock.rb:61:in `__with_ownership'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/input_lock.rb:79:in `with_ownership'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:38:in `start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/repl.rb:15:in `start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-0.10.4/lib/pry/pry_class.rb:169:in `start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/pry-nav-0.2.4/lib/pry-nav/pry_ext.rb:17:in `start_with_pry_nav'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/console/console_command.rb:62:in `start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/console/console_command.rb:17:in `start'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands/console/console_command.rb:97:in `perform'
/Users/user/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
/Users/user/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
/Users/user/.rvm/gems/ruby-2.4.1/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
/Users/user/.rvm/gems/ruby-2.4.1/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/Users/user/Documents/user/project/project/bin/rails:9:in `<top (required)>'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/Users/user/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/Users/user/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Users/user/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
-e:1:in `<main>'
=> nil
1 ответ
AASM не создает метод с именем state
на вашей модели по умолчанию. Раньше много лет назад... но возникали проблемы, когда у наших моделей уже был столбец state
(например, когда это была модель адреса).
Если вы хотите получить доступ к текущему состоянию модели, вам необходимо использовать метод AASM current_state
как это:
item.aasm.current_state
Больше в AASM DOCO здесь