Skip to content
Advertisement

How Select record with where clause with math using ActiveRecord?

I want to return a list of records whose created_at more than 5 seconds before its updated_at

i.e.

Person.where("((people.updated_at - people.created_at) * 60 * 60 * 24) > ?" 5)

But this syntax doesn’t appear to work. Getting the error:

HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Advertisement

Answer

You didn’t say what database you are using, but with MySQL you can the TIMEDIFF function for this:

Person.where("timediff(updated_at, created_at) > ?", '00:00:05')

If you are using Postgres:

Person.where("extract(epoch from updated_at) - extract(epoch from created_at) < ?", 5)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement