algorithm - Rails 3 multiple keywords filtering query -


i writing filtering algorithm takes user input array of keywords,

@keywords = ['news', 'tv show', 'games', 'it'] 

and query table, example, videos table in database. there's string field in table includes couple of tags delimited comma. video instance, if tags field includes one(or more) of keywords, should returned. started

@videos = [] @keywords.each |word|   @videos.push(video.where('tags ?', '%#{word}%')) end @videos = @videos.flatten 

then found, first, it'll include duplicated videos, , second, queries database many times length of keywords not efficient @ all.

any suggestions improve this?

this if need like clause:

@videos = video.where( (@keywords.map { |kw| "tags \'%#{kw}%\'" }).join(" or ") ) 

probably not ruby-esque, straight forward.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -