Rails 查找:条件
我有一个 Reservation
模型,正在使用三个字段搜索该模型。 container_id
必须始终为 self.id
,但作为 confirmed
和 auto_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定我是否明白你的问题,但据我了解,这会起作用:
I'm not sure I'm getting your problem, but from what I understand this would work:
根据您的问题已确认和自动确认,只有一个需要为真。所以使用以下
As per your question confirmed and auto_confirmed only one needs to be true. So use following
我不确定这是否与数据库无关,但你可以尝试
I'm not sure if this is database agnostic, but you could try
你所说的不是真的 - 像这样的查询
将选择两个“确认”列都设置为 1 的行(并且 ActiveRecord 为布尔值创建tinyint 列并检查 1 和 0)。
如果您的意思是“查找与 content_id 匹配的所有行,并且已确认或 auto_confirmed true 但不是两者都为真”,那么您会遇到这样的查询,
您可以用这样的 AR 术语重新表述
,但是,从您的字段的名称实际上是在实现一个具有单独列的状态机,这会给您带来痛苦,因此我会研究一些可以为您提供状态进展的东西,而不是检查单独的打开和关闭位。
What you are saying is not true - a query like
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
which you reword in AR terms like this
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.
我想是这样的:
I think something like this: