Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Ruby on rails 来自单个表的多个引用_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 来自单个表的多个引用

Ruby on rails 来自单个表的多个引用,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我已经看过几次提到这个问题,但都不太完整。我在为单个模型使用联接表时遇到问题。例如,假设我们有用户和HighFive。Highfives将只是两个用户HighFive的联接表。所以我有这个: class Highfive < ActiveRecord::Base belongs_to :user1, :class_name => "User" belongs_to :user2, :class_name => "Us

我已经看过几次提到这个问题,但都不太完整。我在为单个模型使用联接表时遇到问题。例如,假设我们有用户和HighFive。Highfives将只是两个用户HighFive的联接表。所以我有这个:

class Highfive < ActiveRecord::Base
  belongs_to :user1,
             :class_name => "User"

  belongs_to :user2,
             :class_name => "User"
end

class User < ActiveRecord::Base
  has_many :highfives
end
真的,我应该得到这样一个查询:

SELECT "highfives".* FROM "highfives" WHERE "highfives"."user_id" = 1
SELECT "highfives".* FROM "highfives" WHERE "highfives"."user1_id" = 1 or "highfives"."user2_id" = 1
我想要做到这一点,我需要以某种方式修改我的用户模型。但我错过了什么


谢谢。

在您的型号中指定
:外键。所以

class Highfive < ActiveRecord::Base
  belongs_to :user1,
             :class_name => "User",
             :foreign_key => "user1_id"

  belongs_to :user2,
             :class_name => "User",
             :foreign_key => "user2_id"
end

class User < ActiveRecord::Base
  has_many :highfive1, 
           :class_name => "Highfive",
           :foreign_key => "highfive1_id"
  has_many :highfive2, 
           :class_name => "Highfive",
           :foreign_key => "highfive2_id"
end
class Highfive“用户”,
:foreign\u key=>“user1\u id”
属于:user2,
:class_name=>“用户”,
:foreign\u key=>“user2\u id”
结束
类用户“Highfive”,
:外键=>“highfive1\u id”
有很多:highfive2,
:class_name=>“Highfive”,
:外键=>“highfive2\u id”
结束

在您的型号中指定
:外键。所以

class Highfive < ActiveRecord::Base
  belongs_to :user1,
             :class_name => "User",
             :foreign_key => "user1_id"

  belongs_to :user2,
             :class_name => "User",
             :foreign_key => "user2_id"
end

class User < ActiveRecord::Base
  has_many :highfive1, 
           :class_name => "Highfive",
           :foreign_key => "highfive1_id"
  has_many :highfive2, 
           :class_name => "Highfive",
           :foreign_key => "highfive2_id"
end
class Highfive“用户”,
:foreign\u key=>“user1\u id”
属于:user2,
:class_name=>“用户”,
:foreign\u key=>“user2\u id”
结束
类用户“Highfive”,
:外键=>“highfive1\u id”
有很多:highfive2,
:class_name=>“Highfive”,
:外键=>“highfive2\u id”
结束

您需要在
has-many
语句中指定外键,否则Rails将假定它是
用户id

class User < ActiveRecord::Base
  has_many :highfives, :foreign_key => :user1_id
end

您需要在
has\u many
语句中指定外键,否则Rails将假定它是
用户id

class User < ActiveRecord::Base
  has_many :highfives, :foreign_key => :user1_id
end

我相信
外键
应该是
user1id
/
user2id
。我相信
外键
应该是
user1id
/
user2id
。这很简单。不知道为什么我忽略了它。谢谢这很简单。不知道为什么我忽略了它。谢谢