Rails 中的复杂 SQL 查询 - User.where(hash)

发布于 2024-12-05 10:09:03 字数 1465 浏览 0 评论 0原文

我的出发点基本上是 Ryan Bates Railscast

我有用户模型,我需要对其进行一些查询。该模型有几个小时费率属性,如下所示:

#User migration
...
t.decimal :hour_price_high
t.decimal :hour_price_low
...

我的查询以 User.where(Array) 格式工作,其中 Array 已格式化,

["users.hour_price_high <= ? OR users.hour_price_low >= ? OR users.hour_price_low <= ? AND users.hour_price_high >= ?", hour_price_high, hour_price_low, hour_price_low, hour_price_high]

#This is simply a search of two ranges. Search for $40/h - $60/h.
#It will return an User who charge any overlapping price range. Such as one who charges $45/h - $65/h, etc.

我只是希望在 where 语句中将其转换为 Ruby 语法。

我的问题是如何表示 OR。

#This isn't the full query shown above..
User.where(:high_price_high => hour_price_low..hour_price_high, :hour_price_low => hour_price_low..hour_price_high)

生成以下 SQL:

 => "SELECT `users`.* FROM `users` WHERE (`users`.`hour_price_high` BETWEEN 45 AND 60) AND (`users`.`hour_price_low` BETWEEN 45 AND 60)"

当然,由于 AND,这是错误的。我需要它是:

=> "SELECT `users`.* FROM `users` WHERE (`users`.`hour_price_high` BETWEEN 45 AND 60) OR (`users`.`hour_price_low` BETWEEN 45 AND 60)"

如何才能使其成为一个 OR 语句,而不需要从我大二的 EE 课程中删除旧的真值表?

谢谢!

My starting point is basically Ryan Bates Railscast.

I have User model that I need to do some queries on. The model has a couple hourly rate attributes as follows:

#User migration
...
t.decimal :hour_price_high
t.decimal :hour_price_low
...

I have the query working in the User.where(Array) format where Array is formatted

["users.hour_price_high <= ? OR users.hour_price_low >= ? OR users.hour_price_low <= ? AND users.hour_price_high >= ?", hour_price_high, hour_price_low, hour_price_low, hour_price_high]

#This is simply a search of two ranges. Search for $40/h - $60/h.
#It will return an User who charge any overlapping price range. Such as one who charges $45/h - $65/h, etc.

I simply wish to convert this into Ruby syntax in the where statement.

My problem is how to represent the OR.

#This isn't the full query shown above..
User.where(:high_price_high => hour_price_low..hour_price_high, :hour_price_low => hour_price_low..hour_price_high)

Produces this SQL:

 => "SELECT `users`.* FROM `users` WHERE (`users`.`hour_price_high` BETWEEN 45 AND 60) AND (`users`.`hour_price_low` BETWEEN 45 AND 60)"

Which, of course, is wrong because of the AND. I need it to be:

=> "SELECT `users`.* FROM `users` WHERE (`users`.`hour_price_high` BETWEEN 45 AND 60) OR (`users`.`hour_price_low` BETWEEN 45 AND 60)"

How can I make this be an OR statement without busting out the old truth table from my sophomore E.E. classes?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-12-12 10:09:03

当您链接多个 where 方法或在一个 where 方法中有几个参数时,您总是会在它们之间得到 AND,因此当您想要使用 OR 时,您需要使用此语法:

User.where("users.hour_price_high <= ? OR users.hour_price_low >= ? OR users.hour_price_low <= ? AND users.hour_price_high >= ?", hour_price_high, hour_price_low, hour_price_low, hour_price_high)

注意:请观看 http://railscasts.com/episodes/202-active-record-queries-in-rails-3 以获得有关活动记录查询的更多信息。

When you chain several where methods or have a few arguments in one where method, then you always get AND between them, so when you want to have OR you need to use this syntax:

User.where("users.hour_price_high <= ? OR users.hour_price_low >= ? OR users.hour_price_low <= ? AND users.hour_price_high >= ?", hour_price_high, hour_price_low, hour_price_low, hour_price_high)

Note: please watch http://railscasts.com/episodes/202-active-record-queries-in-rails-3 in order to get more information about active record queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文