Rahul Dess
Rahul Dess

Reputation: 2597

How to retrieve only updated records based on `updated_at` column in Active record

I am trying to retrieve all the records from table which are updated. Here i am considering updation of only updated_at column and no other column.

As whenever a new record is created, both created_at and updated_at columns have same timestamp, so i want to get all the records whose updated_at column has changed from the initial assignment.

My query:

@obj = User.where(:receiver_id => current_user.id).where("updated_at - created_at > ?",0).order('created_at DESC'). and getting 'PG::Error: ERROR: operator does not exist: interval > integer'.

My another big question is, Is it valid to find the difference between two columns of the table in single query ?

Please suggest if any good way of doing it other than above method

Upvotes: 2

Views: 1730

Answers (1)

Ruby Racer
Ruby Racer

Reputation: 5740

Make it simpler:

@obj = User.where(:receiver_id => current_user.id).where('updated_at != created_at').order(created_at: :desc)

Upvotes: 2

Related Questions