Created
August 22, 2013 08:03
-
-
Save ys/6304383 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SuggestedUsers | |
attr_reader :user, :options | |
def initialize(user, options = nil) | |
@user = user | |
@options = options || {} | |
end | |
def suggested_users | |
User.joins('JOIN follows as follows_follows ON follows_follows.followee_id = users.id') | |
.where('follows_follows.followee_id NOT IN (SELECT followee_id FROM follows WHERE follower_id = :user_id)', user_id: user.id) | |
.where('follows_follows.follower_id IN (SELECT followee_id FROM follows WHERE follower_id = :user_id)', user_id: user.id) | |
.where('follows_follows.followee_id <> :user_id', user_id: user.id) | |
.order('users.id DESC') | |
.limit(limit).uniq | |
end | |
alias_method :all, :suggested_users | |
def limit | |
options.fetch(:limit, 50) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment