Model relationship problem

Hi,

how do i do a relation for my users table ?
The problem is following:

im building community site, where are users. every user can have a
friend which is user too.
so i created migration which adds to my users table new column user_id,
and i also addes has_many relationship to User model.

so i want that both of them have reference to each other, they are
friends right ? im not sure if i have to user belongs_to too…

  1. is this ok ?
  2. i dont want to use syntax like: user.users, but rather
    user.friends…
    how do i do this ? in rails im forced to create user_id column which is
    accesible through users property in my user model, this is convenction
    over cofiguration and i dont like it in this particular situation.

thanks in advance

regards

Piotr N. wrote:

how do i do a relation for my users table ?
The problem is following:

im building community site, where are users. every user can have a
friend which is user too.
so i created migration which adds to my users table new column user_id,
and i also addes has_many relationship to User model.

so i want that both of them have reference to each other, they are
friends right ? im not sure if i have to user belongs_to too…

  1. is this ok ?
  2. i dont want to use syntax like: user.users, but rather
    user.friends…

You’ll need a self-referential has_many :through setup. I cover how to
do it here:
http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through

how do i do this ? in rails im forced to create user_id column which is
accesible through users property in my user model, this is convenction
over cofiguration and i dont like it in this particular situation.

There are very few conventions in Rails that are forced on you. Keep
reading up and you’ll see how to configure things to work differently
when you need to.


Josh S.
http://blog.hasmanythrough.com