Skip to content

Instantly share code, notes, and snippets.

@ys
Created August 22, 2013 08:03
Show Gist options
  • Save ys/6304383 to your computer and use it in GitHub Desktop.
Save ys/6304383 to your computer and use it in GitHub Desktop.
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