Ruby on rails Rails合并了两个ActiveRecord::Relation';s
我有一个“Profile”模型和该模型的两个ActiveRecord::关系。第一个关系的SQL请求很大。二是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.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版 合并将使用合并关系中的值覆盖第一个关系中相同键的值