-1

Suppose I have a utc_time column which stores float value

I want to select utc_time values is approximate to 1402630856

And I can accept the value x,where abs (x-1402630856) < 3

So I should get the first 5 records,

What should I do by Arel or Active Record ? Thanks

+----+------------------+
| id | utc_time         |
+----+------------------+
|    | 1402630854.99146 |
|    | 1402630856.11391 |
|    | 1402630856.13342 |
|    | 1402630856.13576 |
|    | 1402630857.02345 |
|    | 1402630876.03702 |
|    | 1402630954.7856  |
|    | 1402630954.78688 |
|    | 1402630954.78814 |
|    | 1402630966.06754 |
+----+------------------+
newBike
  • 14,385
  • 29
  • 109
  • 192

1 Answers1

2

This should work in ActiveRecord:

x = 1402630856
Model.where('utc_time - ? between 3 and -3', x)

You can also use this:

Model.where(utc_time: (x - 3)..(x + 3)
Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55