如何引用我的 Friendships 表来有条件地显示代码?
在关注 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用any? 方法检查是否有朋友或inversed_friends。
如果有一条或多条关联记录,它将返回 true,否则它将返回 false
并且您可以使用 inverse_friends 执行完全相同的操作
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
And you can do the exact same thing with inverse_friends