# HABTM: AND on associated table records

**URL:** <https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656>\
**Category:** Rails\
**Created:** [May 11, 2009, 8:52am UTC](https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656 "2009-05-11T08:52:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [May 11, 2009, 8:52am UTC](https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656/1 "2009-05-11T08:52:08Z")

</div>

In my application i have the following models:

Item - has\_and\_belongs\_to\_many :orders  
Order - has\_and\_belongs\_to\_many :items

This is a basic setup for a small shopping app, I can successfully  
add/remove/edit a list of items to one or more orders, but now from the  
administration i need to be able to perform a search like: “show orders  
having (at least) item x and item y and item z (or more)”

Basically, i need to use AND conditions for items belonging to orders,  
something like Order.find(:all, :conditions =\> “items.id = 2 AND  
items.id = 5 AND …”, :include =\> [:items]) .  
Obviously the query above doesn’t work, but I can’t figure out how to do  
that.

Any suggestions?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [May 11, 2009, 2:26pm UTC](https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656/2 "2009-05-11T14:26:18Z")

</div>

I think that the condition that you are looking for is:

:condition =\> item.id in (2,4,6,11,…etc.)

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [May 11, 2009, 4:49pm UTC](https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656/3 "2009-05-11T16:49:49Z")

</div>

Bharat R. wrote:

> I think that the condition that you are looking for is:
> 
> :condition =\> item.id in (2,4,6,11,…etc.)

Thanks for your answer.  
This performs a OR, find all Ordrers having at least one of the ids  
specified inside IN() .

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [May 11, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/habtm-and-on-associated-table-records/167656/4 "2009-05-11T17:52:03Z")

</div>

I think you can do it the other way around, do the find on the items,  
where  
the item id is in one of the set. Then the orders you are looking for  
are  
the ones in the returned rows as item.order. Then you just need to  
select  
the unique orders.

Colin

2009/5/11 Serafino P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
