Rails 4 是否支持 OR 查询
问题描述:
因此在 Rails 4 中添加了使用not
查询的长期期望的功能.
So in Rails 4 the long desired feature to use not
queries has been added.
Article.where.not(title: 'Rails 3')
是否为 或
查询添加了类似的支持,或者他们是否计划实现.我通过浏览发行说明找不到任何内容.
Has similar support been added for or
queries, or are they planning to make it. I couldn't find anything by browsing through the release notes.
显然我试过了
Article.where(title: 'Rails 3').or(title: 'Rails 4')
但这行不通.
答
Article.where(title: ['Rails 3', 'Rails 4'])
就是你在 Active Record 中的做法.
is how you'd do that in Active Record.
不可能使用Rails-y"语法复制任何任意 SQL 查询.但是你总是可以只传入文字 sql.
It's not possible to replicate any arbitrary SQL query using "Rails-y" syntax. But you can always just pass in literal sql.
所以你也可以这样做:
Article.where("articles.title = 'Rails 3' OR articles.title = 'Rails 4'")