我如何引用我的友谊表有条件地显示code? [英] How do I reference my Friendships table to conditionally display code?
问题描述
我已经成功地为用户建立了友谊自我的借鉴协会在我的Ruby on Rails应用程序下面的 Ryan Bates的railscast 。我可以成功地跟随,停止追随,并查看谁在下面。现在,我想在另一个元素拉,我不知道该怎么做。
在我的仪表盘CURRENT_USER页面上我希望有条件地显示基于CURRENT_USER与否的元素下面的人,如果CURRENT_USER被后面的人。在oother也就是说,如果CURRENT_USER没有关注任何人,我不希望显示以下内容:
< DIV ID =下面的类=>
< H2类=TK-约翰母鹿>继< / H>
<%的友谊@ user.friendships%GT;
< DIV CLASS =朋友>
&所述;%=的link_to(friendship.friend.username),friendship.friend%GT; <%=的link_to(取消关注),友谊:方法=> :删除:类=> 取消关注%GT;
< / DIV> <! - #接近 - >
<%结束%GT;
< / DIV> <! - #接近 - >
和如果CURRENT_USER没有被随后的人我不想显示此
< DIV ID =之后,你级=grid_4阿尔法>
< H2类=TK-约翰母鹿>关注< / H>
<%的用户@ user.inverse_friends%GT;
< DIV CLASS =朋友>
<%=的link_to(user.username),用户%GT;
< / DIV> <! - #接近 - >
<%结束%GT;
< / DIV> <! - #接近 - >
这是协会如何设置:
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)
结束
您可以检查是否有任何朋友或inversed_friends与方法的?
如果有一个或多个相关的记录它会返回true,否则这将是错误的。
<%@如果user.friends.any? %GT;
< DIV ID =下面的类=>
<! - 内容 - >
< / DIV>
<%结束%GT;
你可以做inverse_friends同样的事情
<%@如果user.inverse_friends.any? %GT;
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
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? %>
这篇关于我如何引用我的友谊表有条件地显示code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!