开发者

Mutual Relationships with Rails

开发者 https://www.devze.com 2023-03-05 15:56 出处:网络
I\'m following episode 163 of railscasts and it enables user to signify another user is there friend, but that\'s more or less it.

I'm following episode 163 of railscasts and it enables user to signify another user is there friend, but that's more or less it.

I would like to have a helper that finds if a user is their friend.

Get a list of users who are friends of each other.

List of users that have been sent re开发者_StackOverflow社区quests that aren't mutual.

@friends_out = Friendship.where(:user_id=>current_user.id) #users "i" want to be friends with
@friends_in = Friendship.where(:friend_id=>current_user.id) #users who want to be friend with "me"
# users who haven't added "my" id to Friendship and vice versa
# user who are my friend and i'm their friends.

How do I do that?

Thanks.


class User < ActiveRecord::Base
  def friends_with?(user)
    self.friendships.where(:friend_id => user.id).any?
  end

  def friends_with_me?(user)
    user.friendships.where(:friend_id => self.id).any?
  end

  def mutual_friends?(user)
    friends_with?(user) && friends_with_me?(user)
  end
end

Then you could do something like:

current_user.friends_with?(other_user)
0

精彩评论

暂无评论...
验证码 换一张
取 消