Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Activerecord 活动记录合并关系_Activerecord_Ruby On Rails 3.1_Arel - Fatal编程技术网

Activerecord 活动记录合并关系

Activerecord 活动记录合并关系,activerecord,ruby-on-rails-3.1,arel,Activerecord,Ruby On Rails 3.1,Arel,我想知道是否有更好的方法: 我有两个复杂的关系,其中多个连接连接到表D。我想组合/连接它们以获得表D记录。以下方法有效,但有更好的方法吗 class TableD < ActiveRecord::Base def self.relation_three r1 = TableA.relation_one.select("d.id") r2 = TableB.relation_two.select("d.id") where("d.id IN (#{r1.to_sql}) OR

我想知道是否有更好的方法:

我有两个复杂的关系,其中多个连接连接到表D。我想组合/连接它们以获得表D记录。以下方法有效,但有更好的方法吗

class TableD < ActiveRecord::Base

def self.relation_three
  r1 = TableA.relation_one.select("d.id")
  r2 = TableB.relation_two.select("d.id")
  where("d.id IN (#{r1.to_sql}) OR d.id IN (#{r2.to_sql})")
end
谢谢

你能发布实际(或有代表性的)模型吗?
table a.relationship\u one
table b.relationship\u one
table
的关系如何?
TableA.all.joins(:b).select("b.*") # Coerce into TableB class ?