【问题标题】:How to restrict a view by如何限制视图
【发布时间】:2010-11-22 03:42:14
【问题描述】:

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

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

这是关联的设置方式。

友谊.rb

  belongs_to :user
  belongs_to :friend, :class_name => "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

【问题讨论】:

    标签: ruby-on-rails ruby associations


    【解决方案1】:

    我会选择这样的:

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

    【讨论】:

    • 所以我在模型中删除了它,但视图语法是什么? 不起作用。
    • 没关系...我想通了。
    • 顺便说一句,我刚刚问了一个后续问题,您可能有兴趣回答。 stackoverflow.com/questions/4242170/…
    • 其实这有个小问题,如果用户a关注用户b,那么当用户b去用户a的个人资料时,逻辑不会显示关注链接(使用unless)。
    • 又想通了,只是不得不放弃你的陈述的后半部分。
    猜你喜欢
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2017-08-26
    • 2014-10-14
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多