开发者

How to restrict a view by

开发者 https://www.devze.com 2023-01-26 00:59 出处:网络
I have successfully set up a friendship self referencial association for users in my Ruby on Rails app after following Ryan Bates\' railscast. I can successfully follow, unfollow, and view who is foll

I have successfully set up a friendship self referencial association for users in my Ruby on Rails app after following Ryan Bates' railscast. I can successfully follow, unfollow, and view who is following. Now I want to pull in another element and I am not sure how to do it.

When visiting a user/show page of a user that the current_user is already friends with...what is the syntax for checking the Friendships table for an existing friendship.

Here is how the associations are set up.

Friendship.rb

  belongs_to :user
  belongs_to :friend, :class_name => "User"

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

  def friends_workouts
    @friends_w开发者_StackOverfloworkouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order => "created_at DESC", :limit => 3)
  end


I'd go with something like this:

def friends_with?(other_user)
  friends.include?(other_user) || inverse_friends.include?(other_user)
end
0

精彩评论

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