Forum: Ruby on Rails Trying to Create a Friend System

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7015d9823ff88386740b630c89e9971b?d=identicon&s=25 Tony Cosentini (starnix17)
on 2007-06-29 04:50
(Received via mailing list)
I'm trying to add the ability to add other users of my app as friends
(to follow their recent activity, etc.), however I can't grasp what
ActiveRecord association should be used. Basically, I've tried to do

class User < ActiveRecord::Base
  has_one :user_picture
  has_many :scores
  has_many :friendships

class Friendship < ActiveRecord::Base
  belongs_to :user

And my friendships table looks like this:
create_table :friendships, :force => true do |t|
   t.column "user_id",     :integer, :null => false
   t.column "friend_id",   :integer, :null => false

User_id holds the ID of the user that added another user as friend,
and friend_id is the ID of the user that was added as a friend (sorry
if this sentence didn't make much sense).

All I need to do is track who is friends of who, and see if their
friendship is mutual. If I keep working on it, the code above can
probably work, but it doesn't seem to Railsy. So if you can, please
let me know how you would pull this sort of thing off.

Thanks for taking a look,
Aff7605ddfa1b4c6b580089dea653a55?d=identicon&s=25 John Browning (Guest)
on 2007-06-29 09:02
(Received via mailing list)
On 29 Jun 2007, at 03:49, TeeC wrote:

> end
> Tony

I'm no guru on Railsyness, but I wonder if the tables you describe
are capturing all of what's happening in your relationships. Your
last paragraph implies that friendship is mutual -- and thus exists
only when both requested and accepted, requiring two steps to create.
As I read this, the friendships table holds both requests and
accepted friendships. Separating them might make this easier and more
transparent to manage...
This topic is locked and can not be replied to.