Forum: Ruby on Rails has_and_belongs_to_many construction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daan (Guest)
on 2006-04-01 17:21
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.
Josh S. (Guest)
on 2006-04-01 22:03
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...

--
Josh S.
http://blog.hasmanythrough.com
Daan (Guest)
on 2006-04-03 14:14
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
Josh S. (Guest)
on 2006-04-03 20:57
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
This topic is locked and can not be replied to.