Skip to content

Instantly share code, notes, and snippets.

@mayzin00
Last active December 4, 2020 09:00
Show Gist options
  • Save mayzin00/6cbcedd6c6ce7f0eae605e56bd3370f8 to your computer and use it in GitHub Desktop.
Save mayzin00/6cbcedd6c6ce7f0eae605e56bd3370f8 to your computer and use it in GitHub Desktop.
Numeric search using filterrific in rails
If you want to search every keyword including numbers with filterrific in rails, you can use the following search scope.
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.to_s.downcase.split(/\s+/)
terms = terms.map { |e|
('%'+ e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conds = 1
where(
terms.map { |term|
"(LOWER(students.name) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment