Search code examples
sqlruby-on-railspsqlarel

Rails Arel query, get columns a certain duration before now


I have a SQL query that I am trying to construct using Arel to try and check if a timestamp is a certain amount of time ago, something like the following:

SELECT * FROM some_table WHERE updated_at < NOW() - some_table.some_durations;

Specifically I'm unsure of how to insert the NOW() into the query in Arel or specify that a timestamp should be a certain duration in the past. This is as close as I can get, but is obviously wrong:

t = SomeTable.arel_table
t.project(Arel.star).where(t[:created_at].lt(t[:some_durations]))

Solution

  • You should try the following:

    t.project(Arel.star).where(t[:created_at].lt(Arel.sql('NOW() - some_tables.some_durations')))