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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.>> 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) >> > > i could use a has_many :through but i thought i read somewhere i > should try to avoid using them if i didnt need to? good only if > attributes are inside the many-to-many relationshiphatbm is a bit dead these days. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > >> 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) > > > i could use a has_many :through but i thought i read somewhere i > > should try to avoid using them if i didnt need to? good only if > > attributes are inside the many-to-many relationship > > hatbm is a bit dead these days.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---