如何引用我的 Friendships 表来有条件地显示代码?

发布于 2024-10-03 20:34:36 字数 1765 浏览 0 评论 0原文

在关注 Ryan 后,我已在 Ruby on Rails 应用程序中成功为用户建立了友谊自我引用关联贝茨的铁路广播。我可以成功关注、取消关注以及查看谁正在关注。现在我想引入另一个元素,但我不知道该怎么做。

在我的 current_user 仪表板页面上时,我想根据 current_user 是否关注任何人以及 current_user 是否被任何人关注来有条件地显示一个元素。换句话说,如果 current_user 没有关注任何人,我不想显示以下内容:

<div id="following" class="">
 <h2 class="tk-john-doe">Following</h2>
 <% for friendship in @user.friendships %>
  <div class="friend">
   <%= link_to (friendship.friend.username), friendship.friend %> <%= link_to "(Unfollow)", friendship, :method => :delete, :class => "unfollow" %>
   </div> <!-- close # -->
 <% end %>
</div> <!-- close # -->

如果 current_user 没有被任何人关注,我不想显示以下内容:

<div id="following-you" class="grid_4 alpha">
  <h2 class="tk-john-doe">Followers</h2>
  <% for user in @user.inverse_friends %>
    <div class="friend">
      <%= link_to (user.username), user %>
    </div> <!-- close # -->
  <% end %>
 </div> <!-- close # -->

这些是关联的设置方式:

友谊.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

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 on my current_user dashboard page I want to conditionally display an element based on whether or not the current_user is following anyone and if the current_user is being followed by anyone. In oother words, if the current_user is not following anyone I don't want to display the following:

<div id="following" class="">
 <h2 class="tk-john-doe">Following</h2>
 <% for friendship in @user.friendships %>
  <div class="friend">
   <%= link_to (friendship.friend.username), friendship.friend %> <%= link_to "(Unfollow)", friendship, :method => :delete, :class => "unfollow" %>
   </div> <!-- close # -->
 <% end %>
</div> <!-- close # -->

And if the current_user is not being followed by anyone I don't want to display this:

<div id="following-you" class="grid_4 alpha">
  <h2 class="tk-john-doe">Followers</h2>
  <% for user in @user.inverse_friends %>
    <div class="friend">
      <%= link_to (user.username), user %>
    </div> <!-- close # -->
  <% end %>
 </div> <!-- close # -->

These are 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2024-10-10 20:34:36

您可以使用any? 方法检查是否有朋友或inversed_friends。
如果有一条或多条关联记录,它将返回 true,否则它将返回 false

<% if @user.friends.any? %>
<div id="following" class="">
<!-- content -->
</div>
<% end %>

并且您可以使用 inverse_friends 执行完全相同的操作

<% if @user.inverse_friends.any? %>

You can check if there are any friends or inversed_friends with the method any?
It will return true if there are one or more records associated, otherwise it will be false

<% if @user.friends.any? %>
<div id="following" class="">
<!-- content -->
</div>
<% end %>

And you can do the exact same thing with inverse_friends

<% if @user.inverse_friends.any? %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文