【问题标题】:Retrieve method for a polymorphic, has_one and scoped association检索多态、has_one 和作用域关联的方法
【发布时间】:2015-10-21 23:27:19
【问题描述】:

嗨,这有点微不足道,但我一生都无法弄清楚在哪里进行调整。 我有一个 LoanApplication 模型和一个像这样的传输模型:

class LoanApplication < ActiveRecord::Base
  before_save :populate_guid
  belongs_to :user
  has_one :loan, -> { where loan: true }, as: :transferable
  has_one :repayment, -> { where repayment: true }, as: :transferable
  validates_uniqueness_of :guid  

  private 

  def populate_guid
    if new_record?
      while !valid? || self.guid.nil?
        self.guid = SecureRandom.random_number(1_000_000_000).to_s(36)
      end
    end
  end
end

class Transfer < ActiveRecord::Base
  belongs_to :transferable, polymorphic: true 
  belongs_to :user
 validates_presence_of :transferable_id,
                    :transferable_type,
                    :user_id,
                    :amount,
                    :password
end

LoanApplication.first.loan怎么给我下面的错误信息

LoanApplication Load (1.1ms)  SELECT  "loan_applications".* FROM "loan_applications"  ORDER BY "loan_applications"."id" ASC LIMIT 1
NameError: uninitialized constant LoanApplication::Loan

感谢所有见解。 谢谢

【问题讨论】:

    标签: ruby-on-rails-4 scope polymorphic-associations has-one helpermethods


    【解决方案1】:

    我认为 Application 是一个保留字。尝试重命名 LoanApplication?

    【讨论】:

    • 感谢您的输入,不是这样:loan_application 与 application 不同 :)
    【解决方案2】:

    这很简单,我只需要添加 class_name: "Transfer" 即可让一切正常工作。 -__-'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多