展现共同的朋友

时间:2015-09-27 14:40:27

标签: ruby-on-rails

我在社交媒体网站上工作,除了共同的朋友外,我的一切都是友谊。我无法使它发挥作用。

这是我的用户模型:

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => 'User', :foreign_key => 'friend_id'

  validates_presence_of :user_id, :friend_id

  # Create a friendship request.
  def self.send_request(user, friend)
    unless user == friend or Friendship.exists?(user, friend)
      transaction do
        create(:user => user, :friend => friend, :status => 'pending')
        create(:user => friend, :friend => user, :status => 'requested')
      end
    end
  end

  # Accept a friend request.
  def self.accept_request(user, friend)
    transaction do
      accept_one_side(user, friend)
      accept_one_side(friend, user)
    end
  end

  # Decline a request, disconnect or cancel a pending request.
  def self.breakup(user, friend)
    transaction do
      destroy(find_by_user_id_and_friend_id(user, friend))
      destroy(find_by_user_id_and_friend_id(friend, user))
    end
  end

  # Return true if the users are (possibly pending) friends.
  def self.exists?(user, friend)
    not find_by_user_id_and_friend_id(user, friend).nil?
  end

  private
  # Update the db with one side of an accepted friendship request.
  def self.accept_one_side(user, friend)
    request = find_by_user_id_and_friend_id(user, friend)
    request.status = 'accepted'
    request.save!
  end
end

友谊模特:

 function() {
        $(".datepicker").datepicker({
            dateFormat: "dd/mm/yy",
            maxDate : 0,
            changeMonth : true,//this option for allowing user to select month
            changeYear : true, //this option for allowing user to select from year range
            showOn : "button",
            buttonImage : "<c:url value="resources/images/calendar.gif"/>",
            buttonImageOnly : true,
            buttonText : "Select date",
            showOtherMonths : true,
            selectOtherMonths : true
        })

    });

<form:form method="post" id="inbound_form" modelAttribute="formObject" action="submit" onsubmit="return validateInboundAdd()" >
<form:input class="datepicker required" style="height:14px"
                                id="dateReceived" size="10" maxlength="10"  type="text" readonly="readonly" value="${dateReceivedContinue}"
                                path="dateReceived" onfocus="inputFocus(this)" onblur="inputBlur(this)"/>

我试过这个:here但我仍然无法使其发挥作用。

谢谢。

3 个答案:

答案 0 :(得分:0)

鉴于您有两个用户,他们的共同朋友只是:

user1.friends & user2.friends

或者那不是你想要的吗?

这可以轻松地优化它。

user1.friends.where(users: {id: user2.friends.pluck(:id)})

答案 1 :(得分:0)

如果你将共同定义为&#34;共同的朋友&#34;对于用户a和用户b,此方法应该可以解决问题:

class Friendship
  def self.mutual(a,b)
    a.friends.joins('INNER JOIN friendships f2 on friendships.friend_id = f2.friend_id')
             .where('f2.user_id=? and f2.status = ?', b.id, 'accepted')
  end
end

答案 2 :(得分:0)

我认为像这样创建一个单独的函数会更好

    def mutuals(user)
      a_friends = friends
      b_friends = user.friends
      a_friends.select do |friend|
        b_friends.find_by(friend_id: friend.friend_id)
      end
    end

通过这个方法你可以调用@user.mutual(User.find(3))来查找@user变量和id为3的用户之间的共同好友