Ruby on Rails 3:使用多个参数进行搜索?
我有一个搜索框,需要使用查询来搜索 2 个参数,“标题”或“标签”。我可以让一个参数起作用,但不能让两个参数起作用,尝试了“OR”、“||”、“、”都不起作用。
答案是什么?
原始代码: Book.where("title LIKE ?" , "%#{query}%")
我需要什么: Book.where("title LIKE ?" , "%#{查询}%" 或 "标签 LIKE ?", "%#{query}%")
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}%")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该首先拥有“完整”SQL 查询,其中包含占位符 (
?
) 作为where
查询的第一个参数,然后其余参数只是替换占位符。有关详细信息,请参阅Active Record 查询指南。
You should have the "full" SQL query first which contains the placeholders (
?
) as the first argument for thewhere
query and then the remaining arguments are simply the replacements for the placeholders.For more information please see the Active Record Querying guide.
我建议你看一下
metawhere
,它专门用于复杂的查询(我知道这个不是,但值得注意)。请参阅 Railscast。
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.
来自 Rails 指南:
From the Rails Guides: