所以在我当前的项目中,我有一个可以有不同类型事务的文章模型。它有一个主事务,但在某些情况下它可以有多个子事务。
到目前为止,我是这样设置的:
class Article < ActiveRecord::Base
has_one :transaction, inverse_of: :article
has_many :partial_transactions, through: :transaction, source_type: 'MultipleFixedPriceTransaction', source: 'PartialFixedPriceTransaction', inverse_of: :articles
end
class Transaction < ActiveRecord::Base
belongs_to :article, inverse_of: :transaction # Gets inherited to all kinds of Transaction subclasses
end
class MultipleFixedPriceTransaction < Transaction
has_many :children, class_name: 'PartialFixedPriceTransaction', foreign_key: 'parent_id', inverse_of: :parent
end
class PartialFixedPriceTransaction < Transaction
belongs_to :parent, class_name: 'MultipleFixedPriceTransaction', inverse_of: :children
belongs_to :article, inverse_of: :partial_transactions # Overwriting inheritance
end
现在有了这个设置,我有时会收到类似的错误
ActiveRecord::Reflection::ThroughReflection#foreign_key delegated to
source_reflection.foreign_key, but source_reflection is nil:
#<ActiveRecord::Reflection::ThroughReflection:0x00000009bcc3f8 @macro=:has_many,
@name=:partial_transactions, @options={:through=>:transaction,
:source_type=>"MultipleFixedPriceTransaction",
:source=>"PartialFixedPriceTransaction", :inverse_of=>:articles, :extend=>[]},
@active_record=Article(id: integer ...
顺便说一句,我对 source 和 source_type 参数进行了很多实验,其中的参数只是示例。我真的不知道该怎么处理他们。
那么,我怎样才能使这项工作?协会是如何正确建立的?
谢谢你。