2

保存时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成功。宝石有什么问题AASM

4

3 回答 3

2

确保您已通过迁移添加了 AASM 所需的列。

于 2016-11-09T13:51:11.353 回答
0

如果您不想在表上添加列aasm_state

只需添加您的model.rb

aasm_column :your_column
于 2022-01-27T12:21:54.257 回答
0

使用 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 应该可以工作。

于 2021-10-29T19:08:55.777 回答