Hello, I was wondering if there is a way to specify two foreign keys on a has_many association. I have a conversation model with a sender_id and recipient_id. A user can be the sender or recipient of a conversation. To fetch all conversations I must specify the sender_id and recipient_id on the User model, but I can only specify one of the two as shown below. class User < ActiveRecord::Base has_many :conversations, :foreign_key => ''sender_id'' end Is there any way to specify two foreign keys? Thanks! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I don''t think there is... but if you need to encompass that in a single AR association, you could changed the table design to: Users UserConversations user_id conversation_id participation_type Conversations then you could have several different through associations from user to conversation, with or without participation_type. On Dec 22, 3:22 am, elioncho <elion...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I was wondering if there is a way to specify two foreign keys on a > has_many association. I have a conversation model with a sender_id and > recipient_id. A user can be the sender or recipient of a conversation. > To fetch all conversations I must specify the sender_id and > recipient_id on the User model, but I can only specify one of the two > as shown below. > > class User < ActiveRecord::Base > has_many :conversations, :foreign_key => ''sender_id'' > end > > Is there any way to specify two foreign keys? > > Thanks!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
JDevine wrote:> I don''t think there is... > > but if you need to encompass that in a single AR association, you > could changed the table design to: > > Users > > UserConversationsThat should be called Participations or something.> user_id > conversation_id > participation_type > > Conversations > > then you could have several different through associations from user > to conversation, with or without participation_type. > >Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Elias Orozco wrote:> Hello, > > I was wondering if there is a way to specify two foreign keys on a > has_many association. I have a conversation model with a sender_id and > recipient_id. A user can be the sender or recipient of a conversation. > To fetch all conversations I must specify the sender_id and > recipient_id on the User model, but I can only specify one of the two > as shown below. > > class User < ActiveRecord::Base > has_many :conversations, :foreign_key => ''sender_id'' > end > > Is there any way to specify two foreign keys?Sure! You can have two separate associations between the same tables -- or follow JDevine''s suggestion.> > Thanks! > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
another cool way would be to say has_many :conversations, :foreign_key => ''sender_id'', :conditions => ''recipient_id = #{id}'' make sure to use single quotes here, or it won''t evaluate the #{} On Tue, Dec 22, 2009 at 9:13 AM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Elias Orozco wrote: > > Hello, > > > > I was wondering if there is a way to specify two foreign keys on a > > has_many association. I have a conversation model with a sender_id and > > recipient_id. A user can be the sender or recipient of a conversation. > > To fetch all conversations I must specify the sender_id and > > recipient_id on the User model, but I can only specify one of the two > > as shown below. > > > > class User < ActiveRecord::Base > > has_many :conversations, :foreign_key => ''sender_id'' > > end > > > > Is there any way to specify two foreign keys? > > Sure! You can have two separate associations between the same tables -- > or follow JDevine''s suggestion. > > > > > Thanks! > > > > -- > > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > > For more options, visit this group at > > http://groups.google.com/group/rubyonrails-talk?hl=en. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
@ChrisDrappler The problem with this approach is that the conditions params add an AND to the query and it should be an OR. What you suggest does the following: SELECT * FROM `conversations` WHERE (`deals`.sender_id = 3 AND (recipient_id = 3)) And it should be: SELECT * FROM `conversations` WHERE (`deals`.sender_id = 3 OR (recipient_id = 3)) On 22 dic, 10:29, Chris Drappier <chris.drapp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> another cool way would be to say > > has_many :conversations, :foreign_key => ''sender_id'', :conditions => > ''recipient_id = #{id}'' > > make sure to use single quotes here, or it won''t evaluate the #{} > > On Tue, Dec 22, 2009 at 9:13 AM, Marnen Laibow-Koser > <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > > > Elias Orozco wrote: > > > Hello, > > > > I was wondering if there is a way to specify two foreign keys on a > > > has_many association. I have a conversation model with a sender_id and > > > recipient_id. A user can be the sender or recipient of a conversation. > > > To fetch all conversations I must specify the sender_id and > > > recipient_id on the User model, but I can only specify one of the two > > > as shown below. > > > > class User < ActiveRecord::Base > > > has_many :conversations, :foreign_key => ''sender_id'' > > > end > > > > Is there any way to specify two foreign keys? > > > Sure! You can have two separate associations between the same tables -- > > or follow JDevine''s suggestion. > > > > Thanks! > > > > -- > > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > Best, > > -- > > Marnen Laibow-Koser > >http://www.marnen.org > > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > > Posted viahttp://www.ruby-forum.com/. > > > -- > > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > . > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.