Rails activerecord query comparing two attributes of one record

I am looking for a way to create a query that compares two attributes of the same record, without first pulling the record and comparing post-query. In my particular case, there will potentially be 10 thousand notes, so repeating each of them is not an option.

Example: searching for this record by query. where updated_at == created_at

#<User id: 1, name: "xxx", created_at: "2012-07-01 12:00:00", updated_at: "2012-07-01 12:00:00"> 

Rails 3+, ActiveRecord, MySQL.

Update. As the Matzi commentator notes, when using sqlite, the following is valid: mysql :

 User.where("created_at == updated_at") 

Solved : simple error, ruby ​​vs sql.

Sqlite can use:

 User.where("created_at == updated_at") 

but MySQL requires:

 User.where("created_at = updated_at") 
+7
source share
1 answer

If you want to do this in a general way, you must use AREL to get away from a specific database implementation. Something like

 users = User.arel_table User.where(users[:created_at].eq(users[:updated_at])) 

will work

+7
source

All Articles