我正在尝试在个人资料中显示朋友列表,并且我一直在关注railscasts http://railscasts.com/episodes/163-self-referential-association?view=asciicast
因此实现方式大致相同:
用户模型
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
我已经确定了如何查找未决者,以及谁已将我添加到视图中
- if friendship.friend.friends.include?(@user)
= button_to friendship, method: :delete, class: 'btn btn-outline-primary status-friend' do
Remove
- else
%button.btn.btn-outline-primary{disabled: "disabled", type: "button"} Pending
但是我也想表明谁请求了当前用户,但仍然不接受他们。在我的控制器中:
@friendships = @user.friendships
@pending_friendships = @user.inverse_friendships
我需要更改我的方法吗?还是有一些简单的方法而不必每次都检查db来检查友谊和inverse_friendships中是否都存在这种关系?