Я медленно внедряю возможность добавлять и удалять друзей в приложение 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
Любая помощь будет принята с благодарностью.
Отлично работает, спасибо! – will