# Need help with sql condition/join

**URL:** <https://www.ruby-forum.com/t/need-help-with-sql-condition-join/159597>\
**Category:** Rails\
**Created:** [February 6, 2009, 5:53am UTC](https://www.ruby-forum.com/t/need-help-with-sql-condition-join/159597 "2009-02-06T05:53:33Z")\
**Posts on this page:** 2\
**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:** [February 6, 2009, 5:53am UTC](https://www.ruby-forum.com/t/need-help-with-sql-condition-join/159597/1 "2009-02-06T05:53:33Z")

</div>

Hi,  
I’m doing a search, where I have contacts, and also groups for those  
contacts. The association is like this:

contacts:  
has\_many :groupers, :dependent =\> :destroy  
has\_many :groups, :through =\> :groupers

groups:  
has\_many :groupers  
has\_many :contacts, :through =\> :groupers

grouper:  
belongs\_to :group  
belongs\_to :contact

My pagination code looks like this currently:  
if !selected\_group\_id  
@contacts = @current\_user.contacts.paginate(:all, :page =\>  
params[:page], :conditions =\> [‘name LIKE ?’, search\_term], :order =\>  
order\_text)  
else  
@contacts = @current\_user.contacts.paginate(:all, :page =\>  
params[:page],  
:join =\> :groupers,  
:conditions =\> [“groupers.contact\_id = contact.id AND  
groupers.group\_id = #{selected\_group\_id} AND name LIKE ?”, search\_term],  
:order =\> order\_text)  
end

This doesn’t do what I hoped, which is find everything where name is  
like, and there is a grouper with this same contact id and same group  
id. Clearly I’m an sql newbie and don’t know what I’m doing here. Any  
help appreciated.

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:** [February 6, 2009, 7:37am UTC](https://www.ruby-forum.com/t/need-help-with-sql-condition-join/159597/2 "2009-02-06T07:37:04Z")

</div>

Jeff P. wrote:

> Hi,  
> I’m doing a search, where I have contacts, and also groups for those  
> contacts. The association is like this:  
> @contacts = @current\_user.contacts.paginate(:all, :page =\>  
> params[:page],  
> :join =\> :groupers,  
> :conditions =\> [“groupers.contact\_id = contact.id AND  
> groupers.group\_id = #{selected\_group\_id} AND name LIKE ?”, search\_term],  
> :order =\> order\_text)  
> end
> 
> This doesn’t do what I hoped, which is find everything where name is  
> like, and there is a grouper with this same contact id and same group  
> id. Clearly I’m an sql newbie and don’t know what I’m doing here. Any  
> help appreciated.
> 
> thanks,  
> jp

I messed with it some more and came up with something that does work:

```
      @contacts = @current_user.contacts.paginate(:all, :page => 

```

params[:page],  
:joins =\> :groupers,  
:conditions =\> [“groupers.contact\_id = contacts.id AND  
groupers.group\_id = #{selected\_group\_id} AND name LIKE ?”,  
sql\_search\_term],  
:order =\> order\_text)
