Skip to content

Instantly share code, notes, and snippets.

@amirrajabi
Created July 21, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajabi/f05edbb517d54c890f15 to your computer and use it in GitHub Desktop.
Save amirrajabi/f05edbb517d54c890f15 to your computer and use it in GitHub Desktop.
selection of usable module code for Rails development
#---------- General Search -----------#
def self.search keywords
return Media.all if keywords.empty? #if field was empty back all content
search_fields = BASIC_SEARCH_FIELDS #list of table fields
search_array = search_fields.map { |f| "#{f} LIKE ?" } #create new array with "LIKE ?"
search_str = search_array.join(' OR ') #Join all cells of array
search_values = keywords.map { |k| "%#{k}%"} * search_fields.count #create new array and add keyword * field.length
Media.where(search_str, *search_values) # serach in select field on db
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment