Searching multiple fields in table using LIKE


How can I expand the following conditions:

conditions = [“first_name LIKE ?”, “%#{@params[:query]}%”]

to be able to search in other fields in the same database table?

I want to be able to run a query using the same query string, but search
multiple columns such as first_name, email, and comments.

The SQL would be:

SELECT first_name, email, comments FROM contacts WHERE first_name LIKE
“string” OR email LIKE “string” OR comments LIKE “string”



conditions = [“first_name LIKE ? OR email like ?”,
“%#{@params[:query]}%”] etc.


I’m not having luck with that suggestion. I’m getting the following

parse error, unexpected $, expecting kEND


Check your source. You’ve probably got an unclosed begin end block
(syntax error). The way it should work is this:

Model.find(:all, :conditions => [‘id = ? OR id = ?’, id1, id2])

(the example in my previous mail should read ‘:conditions =>’ instead of
‘:conditions =’)


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs