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

android - Inheriting from Theme.AppCompat* -

broadcastreceiver - android BOOT_COMPLETED not received if not activity intent-filter -

basic authentication with http post params android -