ruby-on-rails – 将两个ActiveRecord :: Relation与OR组合,而不
发布时间:2020-12-17 02:15:25 所属栏目:百科 来源:网络整理
导读:a和b是ActiveRecord :: Relation对象,它们返回相同类型的对象(在本例中为Micropost对象) a.class= ActiveRecord::Relationb.class= ActiveRecord::Relationa.first= Micropost(...) b.first= Micropost(...) #They both return the same type of objectsc=a+
a和b是ActiveRecord :: Relation对象,它们返回相同类型的对象(在本例中为Micropost对象)
a.class => ActiveRecord::Relation b.class => ActiveRecord::Relation a.first => Micropost(...) b.first => Micropost(...) #They both return the same type of objects c=a+b c.class => Array #This is not what i'm looking for c=a|b c.class => Array #Not what i'm looking for either c=(a or b) c.class => ActiveRecord::Relation #But it is just a,so it's wrong c==a => true a.merge(b) => [] #It merges with AND which in my case results in an empty array 有没有办法在两个Relation对象中“OR”并在Rails 3.2.11中返回另一个Relation对象?我需要使用一些宝石,如吱吱声来做吗? 如果无法做到:为什么不能这样做?我可以在不加载数据库中的每条记录的情况下对| b生成的数组进行分页吗? 提前致谢. 编辑:我将错误的变量复制到代码块中,b不是ActiveRecord :: Relation,它实际上是一个数组,这就是a.merge(b)返回[]的原因. TLDR:问题是错误的,数组可以分页:) 解决方法
对于你的问题:我可以在不加载数据库中的每条记录的情况下对从| b生成的数组进行分页吗?
是的,你可以对数组进行分页.只有你需要做的是需要’will_paginate / array’文件,你正在使用paginate for array.如果您在很多地方使用,那么最好将它添加到config / initializers中.并且它的数组每次都不会从数据库加载每条记录(即)查询只被触发一次.要加载关联,您可以使用include在阵列上使用eager load. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |