Hi, I have the following 3 models. User , Relationship_type, and Link. User has_many :relationship_types Relationship_type belongs_to :users has_many :links Link belongs_to :relationship_type The Link model has a column :partner. I''d like to find all partners of a user so I''d like to say in User model has_many :partners :through => :relationship_type, :link but that creates errors. How can I do this to be able to say something like userA.partners . Similarly, how can I do the reverse, getting all users that link to a partner , e.g. partnerA.users? In addition, how can I get to User from Link, to say something like linkA.user ? 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.
BTW, the partners are of class User. So from users to partners, it''s many-to-many. From users to relationship_types, or from relationship_types to links, it''s one-to-many. From links to partners, it''s one-to-one. Thanks. On Feb 3, 6:20 pm, Vincent P <ease...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have the following 3 models. User , Relationship_type, and Link. > > User > has_many :relationship_types > > Relationship_type > belongs_to :users > has_many :links > > Link > belongs_to :relationship_type > > The Link model has a column :partner. I''d like to find all partners > of a user so I''d like to say in User model > > has_many :partners > :through => :relationship_type, :link > > but that creates errors. How can I do this to be able to say > something like userA.partners . > > Similarly, how can I do the reverse, getting all users that link to a > partner , e.g. partnerA.users? > > In addition, how can I get to User from Link, to say something like > linkA.user ? > > 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.
Learn by Doing wrote:> Hi, > > I have the following 3 models. User , Relationship_type, and Link. > > User > has_many :relationship_types > > Relationship_typeShould be RelationshipType.> belongs_to :usersShould be :user.> has_many :links > > Link > belongs_to :relationship_type > > The Link model has a column :partner. I''d like to find all partners > of a user so I''d like to say in User model > > has_many :partners > :through => :relationship_type, :link > > but that creates errors. How can I do this to be able to say > something like userA.partnersYou may want to look at the nested_has_many_through plugin.> > Similarly, how can I do the reverse, getting all users that link to a > partner , e.g. partnerA.users? > > In addition, how can I get to User from Link, to say something like > linkA.user ?link.relationship_type.user or you can use :through. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > Thanks.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen! Thank you so much. I''ll take a look at the nested_has_many_through plugin. I needed your guidance: googling for something like that without using the technical term "nested" was fruitless. Vincent. On Feb 3, 7:52 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Learn by Doing wrote: > > Hi, > > > I have the following 3 models. User , Relationship_type, and Link. > > > User > > has_many :relationship_types > > > Relationship_type > > Should be RelationshipType. > > > belongs_to :users > Should be :user. > > has_many :links > > > Link > > belongs_to :relationship_type > > > The Link model has a column :partner. I''d like to find all partners > > of a user so I''d like to say in User model > > > has_many :partners > > :through => :relationship_type, :link > > > but that creates errors. How can I do this to be able to say > > something like userA.partners > > You may want to look at the nested_has_many_through plugin. > > > > > Similarly, how can I do the reverse, getting all users that link to a > > partner , e.g. partnerA.users? > > > In addition, how can I get to User from Link, to say something like > > linkA.user ? > > link.relationship_type.user > or you can use :through. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > > > > Thanks. > > -- > 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.