gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:30:44 29 4
gpt4 key购买 nike

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com