Has_many with 3 models

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.

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.

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-Koser
http://www.marnen.org
[email protected]

Thanks.

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs