Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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合并了两个ActiveRecord::Relation';s_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails合并了两个ActiveRecord::Relation';s

Ruby on rails Rails合并了两个ActiveRecord::Relation';s,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个“Profile”模型和该模型的两个ActiveRecord::关系。第一个关系的SQL请求很大。二是 Profile.where(:id => 1) 我想将第二个关系添加到第一个关系。因此,第二个关系将包含第一个prrofile我试着 first.merge(second) 但它返回空关系。Rails版本是3.2.2 当然,结果也应该是关系。我需要将.limit()和.paginate()添加到此关系。您可以在现有关系上附加.where子句,因此: relation = Th

我有一个“Profile”模型和该模型的两个ActiveRecord::关系。第一个关系的SQL请求很大。二是

Profile.where(:id => 1)
我想将第二个关系添加到第一个关系。因此,第二个关系将包含第一个prrofile

我试着

first.merge(second)
但它返回空关系。Rails版本是3.2.2
当然,结果也应该是关系。我需要将.limit()和.paginate()添加到此关系。

您可以在现有关系上附加.where子句,因此:

relation = Thing.joins(:associated_things => [:users, :still_more_things]).where(:condition => true)
new_relation = relation.where(:id => 1)

这将使用AND将新的“where”与任何现有的“where”结合起来。ActiveRecord::Relation不支持将“Where”与连接,或者,如果需要,请查看squel(https://github.com/ernie/squeel)如果您使用的是Rails 3.1+,或者meta_(https://github.com/ernie/meta_where)3.0版

合并将使用合并关系中的值覆盖第一个关系中相同键的值