0

所以我真的很难做到这一点......基本上我想要做的是保存或创建程序记录时我想检查状态是否设置为已完成。然后如果是,则继续创建关联的事务记录,其中包含来自过程记录的详细信息。但是我一直在*** NoMethodError Exception: undefined method为 nil:NilClass"` 创建一个"`

只是不知道去哪里!!

class Procedure < ActiveRecord::Base
store_accessor :properties, :tooth, :buccal, :mesial, :occlusal, :distal, :lingual

belongs_to :item
belongs_to :patient
belongs_to :practice
belongs_to :provider
belongs_to :operatory
belongs_to :appointment 
belongs_to :transaction

before_create :create_transaction
before_save :create_transaction
before_save :check_if_was_marked_as_complete

validates :item, :patient, :provider, :date, presence: true
validates_associated :item, :patient,  :provider


private

def create_transaction
      if status == "completed"
        transaction.create do |t|
          t.amount = fee
          #t.practice = self.practice
          t.debit_account = patient.family.account
          t.credit_account = provider.revenue_account
          #t.patient = self.patient
         end
   end
end

def check_if_was_marked_as_complete
    if self.status_was == "completed"
        return false
    else
        return true
    end     
end
end

编辑*我的方法现在看起来像这样

def create_association
 if status == "completed"
  create_transaction do |t|
    t.amount = fee
    #t.practice = self.practice
    t.debit_account = patient.family.account
    t.credit_account = provider.revenue_account
    #t.patient = self.patient
  end
    end
end
4

1 回答 1

1

您的交易nil直到它存在为止。所以procedure.transaction #=> nilnil.create #=> undefined method create for nil

要在 belongs_to 关联中构建关联对象,您有以下方法:

  • build_association
  • 创建关联
  • 创建关联!

替换为您的关联对象(即 build_transaction、create_transaction 等)

你应该做的第一件事是重命名你的回调create_association,因为这个方法仍然是由 Rails 定义的。

然后在您的回调中:

create_transaction do |t|
  # ...
end
于 2013-09-11T13:03:04.180 回答