I have a Track model with 4 columns of comma separated string values that need to be searchable by any number of terms.
e.g.
Track.first.genre => "Alternative, Lite Rock, Indie Rock, Psychedelic"
Track.first.mood => "Aggressive, Angry, Dark, Driving, Energetic, Heavy"
Track.first.tempo => "Fast, Medium"
Track.first.artist => "something"
A user then can use any number of terms to narrow down the list of displayed Tracks. I am collecting the terms into arrays matching each column. e.g.
genres = params[:genre].split(",")
moods = params[:mood].split(",")
and so on. So if genres => [“Rock”, “Alternative”], that would match all Tracks which contain either of those terms in the genre column, and all additional terms would select from that returned array, and so on for each of the 4 columns.
What is the best way to do this, and how do I write a where query using like with an array as the condition?
^ Consider using models for genre, mood & tempo. ActiveRecord will make queries that involve those a lot easier.
^ The
serializedeclaration could save you some translating.^ You can just search for the array elements you want with your where call, e.g.:
This means choosing names that aren’t substrings of other names.