Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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 自定义包括rails中的关联_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 自定义包括rails中的关联

Ruby on rails 自定义包括rails中的关联,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有下表 Users(user_id, name, last_name) Relationships(user_id, spouse_id) 我想运行以下查询 person = Relationships.includes(:person).where(:name => 'David') 但是我不希望相关人员被用户id关联。我宁愿它只为这个特定的查询由配偶id关联。Rails完全满足于允许多个关联到同一个表,只要它们使用不同的外键 在这种情况下,您将有如下内容: class User

我有下表

Users(user_id, name, last_name)

Relationships(user_id, spouse_id)
我想运行以下查询

person = Relationships.includes(:person).where(:name => 'David')

但是我不希望相关人员被
用户id
关联。我宁愿它只为这个特定的查询由
配偶id关联。

Rails完全满足于允许多个关联到同一个表,只要它们使用不同的外键

在这种情况下,您将有如下内容:

class User
  has_many :relationships
  has_many :spouses, class_name: "Relationship", foreign_key: :spouse_id
end

用户。首先。配偶
将是所有关系的集合,其中用户的
id
出现在
配偶id
字段中。

看看这个,你能提供模型名称吗,正如我所看到的,缺少了一些东西,这里你定义了用户关系,人呢?你有用户和个人2模型吗?