Created
November 8, 2012 14:58
-
-
Save jeffreybaird/4039285 to your computer and use it in GitHub Desktop.
A user search
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
| scope :search, lambda{|query| | |
| return scoped if query.blank? | |
| tokens = query.split(/ |\+|,/).collect {|c| "%#{c.downcase}%"} | |
| joins(:user).where(["#{(["(lower(user.first_name) like ? \ | |
| or lower(user.last_name) like ? \ | |
| or lower(user.social_security_digits) like ? \ | |
| or lower(users.email) like ? \ | |
| or user.date_of_birth like ? \ | |
| or user.id like ? \ | |
| or user.phone_number like ? \ | |
| or user.first_name && user.last_name like ?)"] * | |
| tokens.size).join(" and ")}", *(tokens * 8).sort]) | |
| } | |
| scope :recently_created, lambda { |created_since = 30.days.ago| | |
| where(['created_at > ?', created_since]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment