Archive for November 22nd, 2009

Search comma separated values db column in Rails

Posted on November 22, 2009, under MySQL, Rails.

I wanted to search for a value in DB and the column data is comma separated values say …

foo,bar,hey,hay

Now I wanted to find a record based on the value …say …hay

MySQL’s FIND_IN_SET comes into play and it works as follows

Model.find(:first, :conditions => ["FIND_IN_SET(?,search_column)", search_string])

Hope this helps!