2

我有这 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模型与模型关联起来,而不需要在它们之间创建新的关联,所以我将以下内容放入测试模型中:UserAnswer

has_one :owner, :through => :answer

但它不起作用,我收到了这个错误

ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Test#owner' on the polymorphic object 'Owner#owner'.

有什么帮助吗?

4

2 回答 2

1

Test

delegate :owner, :to => :answer
于 2012-06-26T04:05:42.410 回答
0

您必须将source_type选项指定为owner多态关联

class Test < ActiveRecord::Base
  belongs_to :answer
  has_one :owner, :through => :answer, :source_type => "User"
end
于 2012-06-26T06:52:33.650 回答