On 21 December 2011 11:04, Fresh Mix <li...@ruby-forum.com> wrote: > Tables: > Users (id, name, sex) > Friendships (user_id, friend_id, status) > > If user(1) and user(2) are friends shoud I add two rows into friendships > table? > > 1, 2, "best friends" > 2, 1, "best friends" > > Or is it enough to have one of these rows? Select * from friendships > where user_id = 2 or friend_id = 2 ???? > > But if I have only one friendship row and I need to list all friens of > user(2) How can I do it? I don't know which one "user_id" or "friend_id" > is needed "data". > > Of course, I can check: IF search_id != friend_id print > (friend_id).user.name ELSE print (user_id).user.name But it is slow and > stupid..
First setup the model relationships so that you are not messing about with id values all the time, something like this railscast [1]. Then if you want all the friends of a user it is simply. user.friends + user.inverse_friends Colin [1] http://railscasts.com/episodes/163-self-referential-association -- 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@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.