Ruby on rails RubyonRails友谊下面总是显示我的名字

Ruby on rails RubyonRails友谊下面总是显示我的名字,ruby-on-rails,view,model,friend,Ruby On Rails,View,Model,Friend,友谊。rb belongs_to :user, :include => [:profile] belongs_to :friend, :class_name => 'user' #following code has_many :friendships has_many :friends, :through => :friendships #followers code has_many :inverse_friendships, :class_name

友谊。rb

belongs_to :user, :include => [:profile]
belongs_to :friend, :class_name => 'user'
  #following code
  has_many :friendships
  has_many :friends, :through => :friendships

  #followers code
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
  has_many :inverse_friends, :through => :inverse_friendships, :source => :user
user.rb

belongs_to :user, :include => [:profile]
belongs_to :friend, :class_name => 'user'
  #following code
  has_many :friendships
  has_many :friends, :through => :friendships

  #followers code
  has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
  has_many :inverse_friends, :through => :inverse_friendships, :source => :user
使用此代码显示我与谁交过朋友以及谁与我交过朋友

查看

   <h2>Friends</h2>
    <ul>
      <% for friendship in @user.friendships.includes(:user) %>
        <li>
          <%= friendship.user.username %>
          (<%= link_to "remove", friendship, :method => :delete %>)
        </li>
      <% end %>
    </ul>

    <p><%= link_to "Find Friends", users_path %></p>

    <h2>Friended by Users</h2>
    <ul>
      <% for user in @user.inverse_friends %>
        <li><%=h user.username %></li>
      <% end %>
    </ul>
朋友
  • (:删除%>)

用户友好
在第一个循环块中,我总是打印我的用户名,我们称它为“fxuser” 在第二个循环块上,我得到了与我交朋友的人的正确用户名


如何解决这一问题,使第一个循环显示我的好友的正确用户名?

在第一个循环中,我相信
友谊。用户
指的是你自己;您正在寻找
友谊。朋友
。另外,我认为您要删除的是
友谊
,而不是删除链接中的相反内容

 <% for friendship in @user.friendships.includes(:friend) %>
    <li>
      <%= friendship.friend.username %>
      (<%= link_to "remove", friendship, :method => :delete %>)
    </li>
 <% end %>