# Has\_and\_belongs\_to\_many construction

**URL:** <https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599>\
**Category:** Rails\
**Created:** [April 1, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599 "2006-04-01T15:21:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 1, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599/1 "2006-04-01T15:21:54Z")

</div>

Hi there,

I’ve got a table construction like this:

presentation contactstate  
Hilton new  
Mc Donalds emailed  
etc. called  
visited  
etc.

Now i’ve created an join table presentationcontactstate with extra  
fields like comment and date time. so i know for example when i called a  
specific presentation.

Now i want to get all the presentations that are in state emailed.

I now got in my contactstate model has\_and\_belongs\_to\_many  
:presentation, :join\_table =\> ‘presentationcontactstates’, :foreign\_key  
=\> ‘fkcontactstateID’, :association\_foreign\_key =\> ‘fkpresentationID’

So with Contactstate.find(:all, conditions =\> ‘contactstate IN  
(1,3,6)’).presentations i’ve got them. But now i want to filter those  
results with for example city = ‘New York’

There i get stuck…

Does anyone know how to do this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 1, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599/2 "2006-04-01T20:03:47Z")

</div>

Daan wrote:

> Now i want to get all the presentations that are in state emailed.  
> …  
> Does anyone know how to do this?

Here’s something I wrote about how to do just that using a join model  
(has\_many :through association).

[http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2](http://blog.hasmanythrough.com/articles/2006/03/01/association-goodness-2)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 3, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599/3 "2006-04-03T12:14:42Z")

</div>

Thanks for you reply but i’ve got the following problem.

//  
In my c= Contactstate.find(:all, conditions =\> ‘contactstate IN  
(1,3,6)’)

i will get an array of contactstates. And each contacstate object got  
presentations. But i want all the presentations. So i want something  
like

Contactstate.find(:all, conditions =\> ‘contactstate IN  
(1,3,6)’).presentations

but now it is

Contactstate.find(:all, conditions =\> ‘contactstate IN  
(1,3,6)’)[0].presentations

---

<div class="post-metadata">

**Author:** ![Daan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@Daan](https://www.ruby-forum.com/u/Daan)\
**Post date:** [April 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/has-and-belongs-to-many-construction/56599/4 "2006-04-03T18:57:54Z")

</div>

Daan wrote:

> Thanks for you reply but i’ve got the following problem.
> 
> //  
> In my c= Contactstate.find(:all, conditions =\> ‘contactstate IN  
> (1,3,6)’)
> 
> i will get an array of contactstates. And each contacstate object got  
> presentations. But i want all the presentations. So i want something  
> like
> 
> Contactstate.find(:all, conditions =\> ‘contactstate IN  
> (1,3,6)’).presentations
> 
> but now it is
> 
> Contactstate.find(:all, conditions =\> ‘contactstate IN  
> (1,3,6)’)[0].presentations

Just replace the condition in my example with one that does the test you  
want:

```
find(:all, :conditions => ["contacts.state IN ?", states])

```

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)
