我正在使用 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/