Module: Census::User::Scopes

Defined in:
lib/census/user.rb

Class Method Summary collapse

Class Method Details

.included(model) ⇒ Object

This scope can be used to find other users who have census answers in common with the given user. The returned list is sorted such that users with the most answers in common are at the beginning of the list.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/census/user.rb', line 53

def self.included(model)
  model.class_eval do
    named_scope :with_matching_census_answers, lambda { |user, limit|
      {
        :select => "DISTINCT users.*, COUNT(answers.id) AS census_match_score",
        :joins => "LEFT JOIN `answers` AS answers ON answers.user_id = users.id LEFT JOIN `answers` AS other_answers ON other_answers.user_id = #{user.id}", 
        :conditions => ['users.id <> ? AND answers.data = other_answers.data AND answers.question_id = other_answers.question_id', user.id],
        :group => 'users.id',
        :order => 'census_match_score DESC',
        :limit => limit
      }
    }
  end
end