如何限制视图

时间:2010-11-22 03:42:15

标签: ruby-on-rails ruby associations

Ryan Bates' railscast之后,我已经成功地在我的Ruby on Rails应用中为用户建立了友情自我参与协会。我可以成功地关注,取消关注,并查看谁在关注。现在我想引入另一个元素,我不知道该怎么做。

访问current_user已经friends的用户的用户/显示页面时...检查Friendships表中现有友谊的语法是什么。< / p>

以下是如何建立关联。

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_workouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order => "created_at DESC", :limit => 3)
  end

1 个答案:

答案 0 :(得分:0)

我会选择这样的事情:

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