Last active
December 4, 2020 09:00
-
-
Save mayzin00/6cbcedd6c6ce7f0eae605e56bd3370f8 to your computer and use it in GitHub Desktop.
Numeric search using filterrific in rails
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
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