Created
July 21, 2015 07:09
-
-
Save amirrajabi/f05edbb517d54c890f15 to your computer and use it in GitHub Desktop.
selection of usable module code for Rails development
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
#---------- 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