我有这3个模型
class User < ActiveRecord::Base
has_many :answers, :as => :owner
end
class Answer < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
has_one :test
end
class Test < ActiveRecord::Base
belongs_to :answer
end
所以我想通过Test
模型将User
模型与Answer
模型相关联,而无需在它们之间创建新关联,因此我将以下内容放入测试模型中:
has_one :owner, :through => :answer
但它不起作用我收到了这个错误
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Test#owner' on the polymorphic object 'Owner#owner'.
任何帮助?
答案 0 :(得分:1)
在Test
:
delegate :owner, :to => :answer
答案 1 :(得分:0)
您必须指定source_type
选项,因为owner
是多态关联
class Test < ActiveRecord::Base
belongs_to :answer
has_one :owner, :through => :answer, :source_type => "User"
end