Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 通过另一个外键关联外键_Mysql_Ruby On Rails_Ruby On Rails 3_Foreign Keys - Fatal编程技术网

Mysql 通过另一个外键关联外键

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

我有这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
但它不起作用,我犯了这个错误

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