Mysql 通过另一个外键关联外键
我有这3种型号Mysql 通过另一个外键关联外键,mysql,ruby-on-rails,ruby-on-rails-3,foreign-keys,Mysql,Ruby On Rails,Ruby On Rails 3,Foreign Keys,我有这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
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
但它不起作用,我犯了这个错误
ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Test#owner' on the polymorphic object 'Owner#owner'.
有什么帮助吗?在测试中:
delegate :owner, :to => :answer
您必须指定source\u type
选项,因为owner
是多态关联
class Test < ActiveRecord::Base
belongs_to :answer
has_one :owner, :through => :answer, :source_type => "User"
end
类测试:答案,:源类型=>“用户”
结束
您是否尝试过has_one:user,:through=>:answer
?是的,它不起作用,如果它起作用,我想与owner打交道,因为我有许多模型充当:owner
class Test < ActiveRecord::Base
belongs_to :answer
has_one :owner, :through => :answer, :source_type => "User"
end