Skip to content

Instantly share code, notes, and snippets.

@jeffreybaird
Created November 8, 2012 14:58
Show Gist options
  • Select an option

  • Save jeffreybaird/4039285 to your computer and use it in GitHub Desktop.

Select an option

Save jeffreybaird/4039285 to your computer and use it in GitHub Desktop.
A user search
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