I''m trying to figure out a way to create this relationship: a user has many friends which are themselves users I have an existing ''users'' table in the database. I''ve created a ''friends'' database with: id: integer user_id: integer friend_id: ineteger I would like to create relationship so I could do something like: friends_array = user.friends where friends_array would be an array of ''user'' objects. -- Posted via http://www.ruby-forum.com/.
Jason, Start here: * http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001836 Robby On Fri, Aug 7, 2009 at 3:06 PM, Jason Burgett<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m trying to figure out a way to create this relationship: > > a user has many friends which are themselves users > > I have an existing ''users'' table in the database. I''ve created a > ''friends'' database with: > > id: integer > user_id: integer > friend_id: ineteger > > I would like to create relationship so I could do something like: > > friends_array = user.friends > > where friends_array would be an array of ''user'' objects. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Robby Russell Chief Evangelist, Partner PLANET ARGON, LLC design // development // hosting w/Ruby on Rails http://planetargon.com/ http://robbyonrails.com/ http://twitter.com/planetargon aim: planetargon +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4068 [fax]
2009/8/7 Jason Burgett <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > I''m trying to figure out a way to create this relationship: > > a user has many friends which are themselves users > > I have an existing ''users'' table in the database. I''ve created a > ''friends'' database with: > > id: integer > user_id: integer > friend_id: ineteger > > I would like to create relationship so I could do something like: > > friends_array = user.friends > > where friends_array would be an array of ''user'' objects. > --These might prove useful http://dizzy.co.uk/ruby_on_rails/contents/self-referential-table-joins http://railsforum.com/viewtopic.php?id=33161 http://github.com/swemoney/has_many_friends/tree/master Colin
Thanks a ton. This one: http://dizzy.co.uk/ruby_on_rails/contents/self-referential-table-joins did the trick! Colin Law wrote:> 2009/8/7 Jason Burgett <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> friend_id: ineteger >> >> I would like to create relationship so I could do something like: >> >> friends_array = user.friends >> >> where friends_array would be an array of ''user'' objects. >> -- > > These might prove useful > http://dizzy.co.uk/ruby_on_rails/contents/self-referential-table-joins > http://railsforum.com/viewtopic.php?id=33161 > http://github.com/swemoney/has_many_friends/tree/master > > Colin-- Posted via http://www.ruby-forum.com/.