简体   繁体   中英

ActiveRecord: Retrieve all records where updated_at is 24 hours or more after created_at

I try to find all records that were updated ( updated_at ) at least 24 hours or more after its creation ( created_at ) using ActiveRecord and a Postgres db.

Any idea how to accomplish this?

You can give a more explicit condition by checking the condition using the hours directly,

Model.where("updated_at >= created_at + interval '24' hour")

returns the query,

SELECT models .* FROM models WHERE (updated_at >= (created_at + interval '22' hour))

Checked this query and its working fine for my models, hope works for you too,

你可以为此写一个范围

scope :scope_name , -> () { where("updated_at::date > created_at::date") } 

You can try by using following query

Model.where("updated_at > (Date(created_at) + integer '1')")

Here is reference

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