我有自我参照协会工作。我的问题是在用户/节目上,我想根据用户与当前用户的关系显示不同的文本。
目前,如果用户 = 当前用户,我将其设置为不显示任何内容。如果用户不是当前用户并且不是当前用户的朋友,我想显示一个链接来关注用户。最后,如果用户不是当前用户并且已经是当前用户的朋友,我想显示文本说“朋友”。
友谊.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
用户/节目
<% unless @user == current_user %>
<%= link_to "Follow", friendships_path(:friend_id => @user), :method => :post %>
<% end %>