2015-08-04 4 views
3

Я медленно внедряю возможность добавлять и удалять друзей в приложение Rails, но мне сложно сменить кнопку «Добавить друга» на «Удалить друга» и наоборот в зависимости от того, дружит ли current_user с @user профиль просматривается в настоящее время.Rails - друзья current_user с @user?

Вот что у меня есть в моей user.rb модели до сих пор (любезно this answer на другой вопрос):

has_many :friendships 
has_many :passive_friendships, :class_name => "Friendship", :foreign_key => "friend_id" 

has_many :active_friends, -> { where(friendships: { approved: true}) }, :through => :friendships, :source => :friend 
has_many :passive_friends, -> { where(friendships: { approved: true}) }, :through => :passive_friendships, :source => :user 
has_many :pending_friends, -> { where(friendships: { approved: false}) }, :through => :friendships, :source => :friend 
has_many :requested_friendships, -> { where(friendships: { approved: false}) }, :through => :passive_friendships, :source => :user 

def friends 
    active_friends | passive_friends 
end 

def friend_with?(user) 
    # ... How would I go about this? 
end 

Любая помощь будет принята с благодарностью.

ответ

3

Вы могли бы определить friend_with? так:

def friend_with?(other_user) 
    friendships.find_by(friend_id: other_user.id) 
end 

Вы можете использовать current_user.friend_with? some_user, чтобы подтвердить два пользователя, являются ли или нет друзей.

Надеюсь, это поможет!

+1

Отлично работает, спасибо! – will

Смежные вопросы