#state-machine #aasm
#конечный автомат #aasm
Вопрос:
При сохранении payment request
появляется сообщение об ошибке:
NoMethodError (undefined method `aasm_state' for #<PaymentRequestx::PaymentRequest:0x007f574d229df8>):
activemodel (4.2.0) lib/active_model/attribute_methods.rb:433:in `method_missing'
aasm (4.11.0) lib/aasm/persistence/base.rb:36:in `aasm_read_state'
aasm (4.11.0) lib/aasm/instance_base.rb:12:in `current_state'
aasm (4.11.0) lib/aasm/persistence/active_record_persistence.rb:233:in `aasm_invalid_state?'
aasm (4.11.0) lib/aasm/persistence/active_record_persistence.rb:225:in `block in aasm_validate_states'
aasm (4.11.0) lib/aasm/persistence/active_record_persistence.rb:223:in `each'
aasm (4.11.0) lib/aasm/persistence/active_record_persistence.rb:223:in `aasm_validate_states'
activesupport (4.2.0) lib/active_support/callbacks.rb:427:in `block in make_lambda'
activesupport (4.2.0) lib/active_support/callbacks.rb:189:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:189:in `block in simple'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
activemodel (4.2.0) lib/active_model/validations.rb:395:in `run_validations!'
activemodel (4.2.0) lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
activemodel (4.2.0) lib/active_model/validations/callbacks.rb:113:in `run_validations!'
activemodel (4.2.0) lib/active_model/validations.rb:334:in `valid?'
activerecord (4.2.0) lib/active_record/validations.rb:58:in `valid?'
activerecord (4.2.0) lib/active_record/validations.rb:83:in `perform_validations'
activerecord (4.2.0) lib/active_record/validations.rb:37:in `save'
activerecord (4.2.0) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.2.0) lib/active_record/transactions.rb:286:in `block (2 levels) in save'
activerecord (4.2.0) lib/active_record/transactions.rb:347:in `block in with_transaction_returning_status'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:220:in `transaction'
activerecord (4.2.0) lib/active_record/transactions.rb:344:in `with_transaction_returning_status'
activerecord (4.2.0) lib/active_record/transactions.rb:286:in `block in save'
activerecord (4.2.0) lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
activerecord (4.2.0) lib/active_record/transactions.rb:285:in `save'
/home/ubuntu/.rvm/gems/ruby-2.2.4/bundler/gems/payment_requestx-7836ca37b30f/app/controllers/payment_requestx/payment_requests_controller.rb:45:in `create'
Ошибка возникает при сохранении в payment_requests_controller.rb
:
if @payment_request.save
Перед этим происходит payment request
успешное сохранение. В чем проблема с gem AASM
?
Ответ №1:
Убедитесь, что вы добавили столбец, требуемый AASM, при переносе.
Ответ №2:
При использовании AASM необходимо включить AASM в целевую модель.
class Job < ActiveRecord::Base
include AASM
aasm do
state :sleeping, initial: true
state :running
event :run, after_commit: :notify_about_running_job do
transitions from: :sleeping, to: :running
end
end
def notify_about_running_job
...
end
end
после этого вам нужно сгенерировать миграцию, чтобы в целевой таблице базы данных модели было это поле aasm_state.
def self.up
add_column :jobs, :aasm_state, :string
end
def self.down
remove_column :jobs, :aasm_state
end
end
после миграции AASM должен работать.
Ответ №3:
Если вы не хотите добавлять столбец aasm_state в свою таблицу.
Просто добавьте в свою model.rb
aasm_column :your_column