ruby-on-rails - rails : Or & and in Rails ActiveRecord where clause

标签 ruby-on-rails ruby-on-rails-3 activerecord

我正在使用 Rails 3.2 并且我有一个数据库表,我想在其中找到符合以下条件的所有行:

a = true and b = true and ( 0< c <1 or d=1), a, b, c, d 是列。

我可以有类似的东西:

 Route.where(:a => true,
             :b => true,
             :c => 0..1 OR :d=1
             ).all          

最佳答案

我可能错了,但我认为您不能使用基于 Arel 的 where 函数来形成该查询;您需要自己形成数据库查询字符串。

假设您使用 SQLite 或 Postgres:

Route.where("a = true and b = true and ((c > 0 and c < 1) or d = 1)").all

我还没有测试过这段代码,但我怀疑这可能适合你。请注意,这是不太“可移植”的代码;如果您更改正在使用的数据库,则查询可能会中断。

关于ruby-on-rails - rails : Or & and in Rails ActiveRecord where clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10958844/

相关文章:

ruby-on-rails - capistrano 3.0.1 在使用 capistrano/rails/assets 时不知道如何构建任务 'starting'

mysql - Rails Mysql - 查找以空格开头的所有值

ruby-on-rails - 形式验证?

sql - 通过关联查询arel

ruby-on-rails - 以编程方式列出所有 cucumber 场景?

ruby-on-rails - 从 S3 获取文件,将它们放入 heroku/tmp 文件夹,解压缩并上传回 s3

ruby-on-rails - 仅获取文件名作为字符串

ruby-on-rails - Rails 3 中重载方法的最佳方法?

ruby-on-rails - 如何从由分隔符 | 分隔的事件记录模型属性名称和值创建字符串?

ruby - 查询去年数据的范围