Has_and_belongs_to_many construction


#1

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.


#2

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


Josh S.
http://blog.hasmanythrough.com


#3

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


#4

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