I have seen the railscast on self-referential relationships here: http://railscasts.com/episodes/163-self-referential-association
I have built upon this in that I've included a 'status' field on friendships so that friendships must be requested and accepted. 'status' is a boolean -- false for not responded to yet, true for accepted.
My problem is in coming up with a method for finding a friendship object given the current_user (I'm using Devise) and another user.
Here is what is available to me:
current_user.friends # lists people you have friended
current_user.inverse_friends # lists people who have friended you
current_user.friendships # lists friendships you've created
current_user.inverse_friendships # lists friendships someone else has created with you
friendship.friend # returns friend in a friendship
I am looking to get a method similar to the following so that I can check the status of friendships easily:
current_user.friendships.with(user2).status
Here's my code: user.rb
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
friendship.rb
belongs_to :user
belongs_to :friend, :class_name => "User"
While I'm at it -- to display a user's friends I have to display both "current_user.friends" and "current_user.inverse_friends" -- is there any way to just be able to call "current_user.friends" and have it be a join of the two?