Combine the two ActiveRecord :: Relations with OR rather than AND, returning Relation rather than an array to be able to paginate pages later

a and b are ActiveRecord :: Relation objects that return objects of the same type (Micropost objects in this case)

 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 wrong c==a => true a.merge(b) => [] #It merges with AND which in my case results in an empty array 

Is there an OR way for both Relation objects and returning another Relation object in Rails 3.2.11? Do I need to use some kind of stone, for example squeel , to do this?

If this is not possible: why is it impossible? Can I split an array into a|b array without loading each record from the database?
If this can be done: can someone write an example of how to do this?

Thanks in advance.

EDIT: I copied the wrong variables into a code block, b was not ActiveRecord :: Relation, it was actually an array, and that is why a.merge (b) returned []. My mistake.

TL; DR: the question is incorrect and the array may be paginated :)

+4
source share
1 answer

For the question: Can I paginate the array resulting from a|b without loading every record from the database?

Yes , you can paginate an array . Only you need to make the require 'will_paginate/array' file in which you use paginate for array . If you use in many places, it is best to add it to config/initializers . And its array, therefore, it will not load every record from the database every time (i.e.) The request is launched only once. To load an association, you can use eager load on array with include .

+2
source

All Articles