Skip to content

Instantly share code, notes, and snippets.

@mindaslab
Last active December 19, 2015 14:49
Show Gist options
  • Save mindaslab/5971852 to your computer and use it in GitHub Desktop.
Save mindaslab/5971852 to your computer and use it in GitHub Desktop.
Active Record Search For Rails 3, 4
##
# 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