# Needing some help with many to many query

**URL:** <https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569>\
**Category:** Rails\
**Created:** [October 29, 2007, 2:35am UTC](https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569 "2007-10-29T02:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [October 29, 2007, 2:35am UTC](https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569/1 "2007-10-29T02:35:09Z")

</div>

Hey i have a many to many relationship between Users &  
PrivmessageThreads. This is where users will have many threads of  
privmsgs, and vice versa. I trying to do a query on a PrivmsgThread  
to just find the other user\_id’s in the thread.

something similar to :  
select \* from PrivmsgsThreads\_Users WHERE privmsg\_thread\_id = 1 &&  
user\_id != (current\_user\_id)

im using has\_and\_belongs\_to\_many in both my users and privmsgthread  
model, but dunno how to do a query like this.

any help?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [October 29, 2007, 10:09am UTC](https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569/2 "2007-10-29T10:09:43Z")

</div>

On 29 Oct 2007, at 01:34, Chubbs wrote:

> Hey i have a many to many relationship between Users &  
> PrivmessageThreads. This is where users will have many threads of  
> privmsgs, and vice versa. I trying to do a query on a PrivmsgThread  
> to just find the other user\_id’s in the thread.
> 
> something similar to :  
> select \* from PrivmsgsThreads\_Users WHERE privmsg\_thread\_id = 1 &&  
> user\_id != (current\_user\_id)

Well you could do exactly that.

> im using has\_and\_belongs\_to\_many in both my users and privmsgthread  
> model, but dunno how to do a query like this.

If you were using has\_many :through, then you could just do  
thread.thread\_readers.user\_ids (assuming thread\_readers was your  
through table)

Fred

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [October 30, 2007, 4:16am UTC](https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569/3 "2007-10-30T04:16:06Z")

</div>

On 29 Oct 2007, at 09:57, Chubbs wrote:

Please keep things on list.

> > > something similar to :  
> > > select \* from PrivmsgsThreads\_Users WHERE privmsg\_thread\_id = 1 &&  
> > > user\_id != (current\_user\_id)
> > 
> > Well you could do exactly that.
> 
> how would i go about doing it this way?

Well if you’ve got a raw chunk of sql,  
self.class.connection.select\_values ‘sql’

Assuming t is your thread, why not just t.user\_ids and then just  
exclude the ‘bad’ user id later on.

> attributes are inside the many-to-many relationship  
> hatbm is a bit dead these days.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [October 30, 2007, 4:39am UTC](https://www.ruby-forum.com/t/needing-some-help-with-many-to-many-query/118569/4 "2007-10-30T04:39:30Z")

</div>

ok thanks for your help,

ill use the has\_many :through and see how it goes,

thanks for advice

On Oct 29, 9:12 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
