Rails: как заставить has_many: через ассоциацию работать с Single Table Inheritance
Так что в моем текущем проекте у меня есть модель Article, которая может иметь различные виды транзакций. Он имеет одну основную транзакцию, но при определенных обстоятельствах может иметь несколько подопераций.
До сих пор я настраивал это так:
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, и это только примеры. Я действительно не знаю, что с ними делать.
Итак, как я могу сделать эту работу? Как правильно настроить ассоциацию?
Спасибо.