简体   繁体   English

ActiveRecord-比较同一行中的两个值

[英]ActiveRecord - Compare two values in same row

I have a table of call data and I want to query all unanswered calls, which means that the call start time is equal to the call end time. 我有一张通话数据表,我想查询所有未应答的通话,这意味着通话开始时间等于通话结束时间。 I currently use the following plain SQL which works as expected: 我目前使用下面的普通SQL可以正常工作:

select * from calls where calls.start = calls.end

I was wondering if there is a more "rails" way to do this using the ActiveRecord Query Interface. 我想知道是否有使用ActiveRecord查询接口的其他“轨道”方法。 Ideally I'd like to set up a scope in my Call model that returns me all unanswered calls. 理想情况下,我想在我的“ Call模型中设置一个范围,以将所有未应答的呼叫返回给我。 Something like: 就像是:

scope :unanswered, -> { where(start: :end) }

The above doesn't work because Rails treats :end as a string instead of the end column in the DB. 上面的方法不起作用,因为Rails将:end视为字符串而不是数据库中的end列。

I'm using PostgreSQL as my DB engine. 我使用PostgreSQL作为数据库引擎。

The SQL query SQL查询

select * from calls where calls.start = calls.end

could be done in a rails way using a scope as follows: 可以使用如下所示的范围以rails方式完成:

scope :unanswered, -> { where('start = end') }

我认为您可以执行以下操作:

scope :unanswered, -> (end) { where(start: end) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM