# Timestamped records - including one off the front

**URL:** <https://www.ruby-forum.com/t/timestamped-records-including-one-off-the-front/185804>\
**Category:** Rails\
**Created:** [March 26, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/timestamped-records-including-one-off-the-front/185804 "2010-03-26T12:54:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 26, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/timestamped-records-including-one-off-the-front/185804/1 "2010-03-26T12:54:16Z")

</div>

Hi

I have a table containing timestamped weather data. When I wish to  
show the weather for a day I use a named scope:  
named\_scope :by\_time\_range, lambda{ |time\_min, time\_max|  
{:conditions =\> [‘measured\_at \>= ? and measured\_at \<= ?’,  
time\_min.utc, time\_max.utc] } }  
where time\_min and max are midnight to midnight. If there is a sample  
at the midnight at the start of the day then all is well, but if there  
is not then I have no reference point for the start of the day, so I  
would like to include in the find the last sample before midnight if  
there is not one at midnight.

I can see how to do this as two finds, one finding the samples where  
measured\_at \> time\_min and \<= time\_max and then concatenating this  
with the last sample from a find where measured\_at \<= time\_min, which  
will give me the one at midnight or the last one before midnight.

Is there a way I can do this within one find?

Colin
