jakobk
jakobk

Reputation: 1132

Ruby on rails 3: Search with multiple params?

I have a search box, and need to search through 2 parameters, "title" or "tags" using the query. I can get one parameter to work, but not two, tried 'OR', '||', ',' nothing works.

Whats the answer ?

Original code: Book.where("title LIKE ?" , "%#{query}%")

What I need: Book.where("title LIKE ?" , "%#{query}%" OR "tags LIKE ?" , "%#{query}%")

Upvotes: 2

Views: 2470

Answers (3)

apneadiving
apneadiving

Reputation: 115511

I suggest you take a look at metawhere which is dedicated to complicated queries (I know this one isn't but it worth noticing).

See Railscast.

Upvotes: 2

Ryan Bigg
Ryan Bigg

Reputation: 107718

Book.where("title LIKE ? OR tags like ?" , "%#{query}%", "%#{query}%")

You should have the "full" SQL query first which contains the placeholders (?) as the first argument for the where query and then the remaining arguments are simply the replacements for the placeholders.

For more information please see the Active Record Querying guide.

Upvotes: 7

topek
topek

Reputation: 18979

From the Rails Guides:

Client.where("orders_count = ? AND locked = ?", params[:orders], false)

Upvotes: 1

Related Questions