简体   繁体   中英

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:

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. Ideally I'd like to set up a scope in my Call model that returns me all unanswered calls. 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.

I'm using PostgreSQL as my DB engine.

The SQL query

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

could be done in a rails way using a scope as follows:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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