如何限制视图

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

当访问用户的用户/显示页面时, current_user已经是friends …用于检查现有友谊的Friendships表的语法是什么。

以下是如何建立关联。

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 

我会用这样的东西:

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