# Conditions and joins and paginate, oh my!

**URL:** <https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996>\
**Category:** Rails\
**Created:** [December 19, 2008, 4:33am UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996 "2008-12-19T04:33:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 19, 2008, 4:33am UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996/1 "2008-12-19T04:33:04Z")

</div>

I’m starting off with something that already works, and trying to add  
another condition to it. I just can’t seem to find an example of the  
proper syntax to make this happen.

In this case, I have users, and users have many contacts and contacts  
have and belong to many groups. Up til now I’ve just been searching for  
(and paginating) a set of contacts that belong to this user that are  
“LIKE” my search term. Now I want to also limit the selection by which  
groups a contact belongs to.

So far I’ve been finding a set of contacts to show like this:

@contacts = @current\_user.contacts.paginate(:all, :conditions =\> [“name  
LIKE ?”, search\_term])

Now I want to add to that, a selection based on a group, so, in pseudo  
syntax that might look something like this:

@contacts = @current\_user.contacts.paginate(  
:all,  
:conditions =\> [“name LIKE ?”,  
search\_term],  
:joins =\> :group,  
:more\_conditions =\> {:groups =\>  
{:group.id =\> selected\_group}})

How do I hook these two different types of conditions together?

Thanks!

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 19, 2008, 8:23am UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996/2 "2008-12-19T08:23:06Z")

</div>

Jeff,  
Visit this page, it will really help you in solving your  
problem

> **[Opera News](https://blogs.opera.com/news/)**
>
> The latest news about Opera web browsers, tech trends, internet tips.

In case of any queries you can reply on that blog or on this form any  
time.

Thanks & Regards,  
Shahroon Ali K.

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 19, 2008, 8:29am UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996/3 "2008-12-19T08:29:12Z")

</div>

shoni khan wrote:

> Jeff,  
> Visit this page, it will really help you in solving your  
> problem  
> [The Opera Blog - News | Opera](http://my.opera.com/learnror/blog/pagination-with-complex-queries)  
> In case of any queries you can reply on that blog or on this form any  
> time.
> 
> Thanks & Regards,  
> Shahroon Ali K.

thanks very much. I will check it out.

jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 19, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996/4 "2008-12-19T17:02:16Z")

</div>

Frederick C. wrote:

> On 19 Dec 2008, at 07:29, Jeff P. wrote:
> 
> > > Shahroon Ali K.
> > 
> > thanks very much. I will check it out.
> 
> It is however terrible advice. Fetching all rows into memory and then  
> paginating that array could absolutely kill you.
> 
> You can merge conditions with the merge\_conditions class method on  
> ActiveRecord::Base (which is private on 2.1, but not 2.2. You can  
> always call it from one of your model’s class methods. Or you can just  
> build up array based conditions by hand.
> 
> Fred

Thanks Fred,  
Will that merge\_conditions method deal with different styles of  
conditions?, like:  
:conditions =\> [“name LIKE ?”, search\_term]  
and  
conditions =\> {:groups =\> {:group.id =\> selected\_group}}

I don’t know how to translate either one of these into the other form.

thanks,  
jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [December 19, 2008, 11:35am UTC](https://www.ruby-forum.com/t/conditions-and-joins-and-paginate-oh-my/155996/5 "2008-12-19T11:35:18Z")

</div>

On 19 Dec 2008, at 07:29, Jeff P. wrote:

> > Shahroon Ali K.
> 
> thanks very much. I will check it out.

It is however terrible advice. Fetching all rows into memory and then  
paginating that array could absolutely kill you.

You can merge conditions with the merge\_conditions class method on  
ActiveRecord::Base (which is private on 2.1, but not 2.2. You can  
always call it from one of your model’s class methods. Or you can just  
build up array based conditions by hand.

Fred
