[英]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. 我有一个搜索框,需要使用查询搜索2个参数“title”或“tags”。 I can get one parameter to work, but not two, tried 'OR', '||', ',' nothing works. 我可以得到一个参数,但不是两个,试过'OR','||',','没什么用。
Whats the answer ? 答案是什么 ?
Original code: Book.where("title LIKE ?" , "%#{query}%")
原始代码: Book.where("title LIKE ?" , "%#{query}%")
What I need: Book.where("title LIKE ?" , "%#{query}%" OR "tags LIKE ?" , "%#{query}%")
我需要什么: Book.where("title LIKE ?" , "%#{query}%" OR "tags LIKE ?" , "%#{query}%")
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. 您应首先使用“完整”SQL查询,其中包含占位符( ?
)作为where
查询的第一个参数,然后其余参数只是占位符的替换。
For more information please see the Active Record Querying guide . 有关详细信息,请参阅Active Record查询指南 。
I suggest you take a look at metawhere
which is dedicated to complicated queries (I know this one isn't but it worth noticing). 我建议你看一下专门用于复杂查询的metawhere
(我知道这个不是,但值得注意)。
来自Rails指南 :
Client.where("orders_count = ? AND locked = ?", params[:orders], false)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.