如何限制由视图 [英] How to restrict a view by

查看:188
本文介绍了如何限制由视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地为用户建立了友谊自我的借鉴协会在我的Ruby on Rails应用程序下面的 Ryan Ba​​tes的railscast 。我可以成功地跟随,停止追随,并查看谁在下面。现在,我想在另一个元素拉,我不知道该怎么做。

当访问一个用户的 CURRENT_USER 已经朋友的用户/显示页面...什么对于检查友谊表中现有的友谊。

语法

下面是协会是如何设置的。

Friendship.rb

  belongs_to的:用户
  belongs_to的:朋友:CLASS_NAME => 用户

User.rb

 的has_many:友谊
  的has_many:朋友:通过=> :友谊
  的has_many:inverse_friendships,:CLASS_NAME => 友谊,:foreign_key => friend_id
  的has_many:inverse_friends,:通过=> :inverse_friendships,:源=> :用户  高清friends_workouts
    @friends_workouts || = Workout.find_all_by_user_id(self.friends.map(安培;:ID):为了=>中created_at DESC:极限=> 3)
  结束


解决方案

我会去的东西是这样的:

 高清friends_with?(other_user)
  friends.include?(other_user)|| inverse_friends.include?(other_user)
结束

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_workouts ||= 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

这篇关于如何限制由视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆