Cyclic "habtm" associations?

Although risking a stupid question, I have the following “challenge”: In
a simple community portal users have some sort of “is-contact-of”
relation to other users.

I planned to model this by

class User
has_and_belongs_to :users
end

which of course doesn’t work, because the join table

create table users_users (
user_id int not null,
user_id int not null,

foreign key (user_id) references users (id),
foreign key (user_id) references users (id),
primary key (user_id, user_id)

) type = InnoDB, character set utf8;

cannot be created in MySQL.

How would I model that best?

Starburger

On 2/1/07, starburger [email protected] wrote:

cannot be created in MySQL.

How would I model that best?

Starburger

Use a join table Contacts with user_id and contact_id fields. Then
use a has_many :through association to find a user’s contacts.

Also, check out this post and the comments that follow:
http://blog.hasmanythrough.com/2006/4/21/self-referential-through

Hope this helps,


Zack C.
http://depixelate.com

Zack C. wrote:

On 2/1/07, starburger [email protected] wrote:

cannot be created in MySQL.

How would I model that best?

Starburger

Use a join table Contacts with user_id and contact_id fields. Then
use a has_many :through association to find a user’s contacts.

Also, check out this post and the comments that follow:
http://blog.hasmanythrough.com/2006/4/21/self-referential-through

Hope this helps,


Zack C.
http://depixelate.com

Thanks. That helped. A detailleed example for self-referential has_many
can be found at
http://www.aldenta.com/2006/11/10/has_many-through-self-referential-example/

Starburger

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