Dun Peal - 11 months ago 49

SQL Question

I have a Rails 4

`ActiveRecord`

`DateTime`

`dt1`

`dt2`

How do I retrieve all instances of this model for which

`dt2`

`dt1`

Answer

Try this:

```
Model.where(%q{ dt2 - dt1 < interval '18 hours' })
```

`18 hours`

can be a variable you assign dynamically through a param.

```
dt_diff = '22 days' # or 18 hours or 3 years
Model.where(%q{ dt2 - dt1 < interval dt_diff })
```

Source (Stackoverflow)