Rails 查找:条件

发布于 2024-08-31 01:57:32 字数 368 浏览 2 评论 0原文

我有一个 Reservation 模型,正在使用三个字段搜索该模型。 container_id 必须始终为 self.id,但作为 confirmedauto_confirmed 只需要其中一个为 true。我有以下内容,但它没有执行我需要的功能:

Reservation.find(:all, 
:conditions => ['container_id = ? AND confirmed = ? OR auto_confirm = ?', 
self.id, true, true,])

我应该如何更改它?

I have a Reservation model that I'm searching for with three fields. The container_id must always be self.id but as confirmed and auto_confirmed only one needs to be true. I have the following but it doesn't perform what I need:

Reservation.find(:all, 
:conditions => ['container_id = ? AND confirmed = ? OR auto_confirm = ?', 
self.id, true, true,])

How should I change this?

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

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

发布评论

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

评论(5

燃情 2024-09-07 01:57:32

我不确定我是否明白你的问题,但据我了解,这会起作用:

Reservation.find(:all, 
:conditions => ['container_id = ? AND (confirmed = ? OR auto_confirm = ?)', 
self.id, true, true,])

I'm not sure I'm getting your problem, but from what I understand this would work:

Reservation.find(:all, 
:conditions => ['container_id = ? AND (confirmed = ? OR auto_confirm = ?)', 
self.id, true, true,])
丑丑阿 2024-09-07 01:57:32

根据您的问题已确认和自动确认,只有一个需要为真。所以使用以下

Reservation.find(:all, 
                 :conditions => ['container_id = :container AND 
                  ( (confirmed = :flag and auto_confirm != :flag) ||
                    (confirmed != :flag and auto_confirm = :flag))', 
                    {:container=> self.id, :flag=>true}]
               )

As per your question confirmed and auto_confirmed only one needs to be true. So use following

Reservation.find(:all, 
                 :conditions => ['container_id = :container AND 
                  ( (confirmed = :flag and auto_confirm != :flag) ||
                    (confirmed != :flag and auto_confirm = :flag))', 
                    {:container=> self.id, :flag=>true}]
               )
箜明 2024-09-07 01:57:32

我不确定这是否与数据库无关,但你可以尝试

Reservation.find(:all, 
:conditions => ['container_id = ? AND confirmed = ? **XOR** auto_confirm = ?', 
self.id, true, true,])

I'm not sure if this is database agnostic, but you could try

Reservation.find(:all, 
:conditions => ['container_id = ? AND confirmed = ? **XOR** auto_confirm = ?', 
self.id, true, true,])
提笔落墨 2024-09-07 01:57:32

你所说的不是真的 - 像这样的查询

  SELECT * FROM foos WHERE content_id = 345 AND (confirm = 1 OR auto_confirm = 1)

将选择两个“确认”列都设置为 1 的行(并且 ActiveRecord 为布尔值创建tinyint 列并检查 1 和 0)。

如果您的意思是“查找与 content_id 匹配的所有行,并且已确认或 auto_confirmed true 但不是两者都为真”,那么您会遇到这样的查询,

 SELECT * FROM foos WHERE content_id = 345 AND ((confirmed = 1 AND auto_confirm = 0) OR (confirmed = 0 AND auto_confirm = 1))

您可以用这样的 AR 术语重新表述

  Reservation.find(:all, 
    :conditions => [
      'container_id = ? AND ((confirmed = 1 AND auto_confirm != 1) OR (confirmed = 0 AND auto_confirm != 1))', 
      self]
  )

,但是,从您的字段的名称实际上是在实现一个具有单独列的状态机,这会给您带来痛苦,因此我会研究一些可以为您提供状态进展的东西,而不是检查单独的打开和关闭位。

What you are saying is not true - a query like

  SELECT * FROM foos WHERE content_id = 345 AND (confirm = 1 OR auto_confirm = 1)

will select the rows where both "confirm" columns are set to 1 (and ActiveRecord creates tinyint columns for booleans and checks against 1 and 0).

If you mean "find all rows matching on the content_id and having EITHER confirmed OR auto_confirmed true but NOT both" then you come down to a query like this

 SELECT * FROM foos WHERE content_id = 345 AND ((confirmed = 1 AND auto_confirm = 0) OR (confirmed = 0 AND auto_confirm = 1))

which you reword in AR terms like this

  Reservation.find(:all, 
    :conditions => [
      'container_id = ? AND ((confirmed = 1 AND auto_confirm != 1) OR (confirmed = 0 AND auto_confirm != 1))', 
      self]
  )

However, judging from the names your fields have you are actually implementing a state machine with separate columns which will bring you pain, so I'd investigate something that would give you progressions of states instead of checking for separate on and off bits.

被你宠の有点坏 2024-09-07 01:57:32

我想是这样的:

Reservation.find(:all, 
:conditions => ['container_id = ? AND ((confirmed != true AND auto_confirm = true) OR (confirmed = true AND auto_confirm != true))', 
self.id])

I think something like this:

Reservation.find(:all, 
:conditions => ['container_id = ? AND ((confirmed != true AND auto_confirm = true) OR (confirmed = true AND auto_confirm != true))', 
self.id])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文