我无法将这些模型联系在一起。
class LongTermFinanceScorecard < Scorecard
has_one :access_to_finance, foreign_key: :scorecard_id
has_one :insurance_policy, through: :access_to_finance, class_name: 'LongTermAccessToFinance'
end
class AccessToFinance < ActiveRecord::Base
belongs_to :scorecard
end
class LongTermAccessToFinance < AccessToFinance
has_one :insurance_policy, foreign_key: 'access_to_finance_id'
end
class InsurancePolicy < ActiveRecord::Base
belongs_to :access_to_finance, class_name: 'LongTermAccessToFinance'
belongs_to :scorecard
end
class LongTermInsurancePolicy < InsurancePolicy
end
因此,当创建LongTermFinanceScorecard
时,会构建一个LongTermAccessToFinance
来构建LongTermInsurancePolicy
。
LongTermAccessToFinance
继承自AccessToFinance
和LongTermInsurancePolicy
,分别继承自InsurancePolicy
。
如果我在rails控制台中运行,我会得到以下结果。
s = LongTermFinanceScorecard.create
=> true
s.access_to_finance
=> true
s.access_to_finance.insurance_policy
=> true
i = s.access_to_finance.insurance_policy
=> true
i.scorecard == s
=> true #This shows that the relationship works backwards
s.insurance_policy
=> ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :insurance_policy or :insurance_policy in model AccessToFinance. Try 'has_many :insurance_policy, :through => :access_to_finance, :source => <name>'. Is it one of :scorecard
错误的Could not find the source association(s) :insurance_policy or :insurance_policy in model AccessToFinance.
部分表明,当AccessToFinance
实际属于LongTermAccessToFinance
时,它会在AccessToFinance
中查找insurance_policy。
如何在关联中引用:class_name
Specify the class name of the association. Use it only if that name can't be inferred from the association name. So has_one :manager will by default be linked to the Manager class, but if the real class name is Person, you'll have to specify it with this option.
:through
Specifies a Join Model through which to perform the query. Options for :class_name, :primary_key, and :foreign_key are ignored, as the association uses the source reflection. You can only use a :through query through a has_one or belongs_to association on the join model.
:source
Specifies the source association name used by has_one :through queries. Only use it if the name cannot be inferred from the association. has_one :favorite, through: :favorites will look for a :favorite on Favorite, unless a :source is given.
:source_type
Specifies type of the source association used by has_one :through queries where the source association is a polymorphic belongs_to.
对象的类型?
修改
在Ruby on Rails API中,我在has_one:association
的选项下找到了这个class_name:
所以好像AccessToFinance
似乎不起作用,因为它仍在寻找LongTermAccessToFinance
而不是char registered = '®';
System.out.println("Answer:"+Integer.toHexString(registered));
的关联