Last active
December 19, 2015 14:49
-
-
Save mindaslab/5971852 to your computer and use it in GitHub Desktop.
Active Record Search For Rails 3, 4
This file contains 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
## | |
# A simple search method | |
def self.search text | |
columns = [:name, :roll_no, :address, :city, :pin, :ph] | |
words = text.downcase.split(/\s+/) | |
query_array_2 = [] | |
for word in words | |
query_array = [] | |
for column in columns | |
query_array << "lower(#{column}) like '%#{word}%'" | |
end | |
query_array_2 << query_array.join(' or ') | |
end | |
query = query_array_2.collect{|e| "(#{e})"}.join(' and ') | |
self.where query | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment