# Date Range comparison

**URL:** <https://www.ruby-forum.com/t/date-range-comparison/158117>\
**Category:** Rails\
**Created:** [January 19, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/date-range-comparison/158117 "2009-01-19T14:54:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [January 19, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/date-range-comparison/158117/1 "2009-01-19T14:54:55Z")

</div>

I have a set of dates in my database and would like to only get the  
latest one or the one closest to the current date. I’m trying to come up  
with a way of doing it but had no luck. If any one could help I would  
appreciate it.

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [January 19, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/date-range-comparison/158117/2 "2009-01-19T16:14:32Z")

</div>

Sort by date with a limit of one? Hopefully you have an index on that  
field?

On Jan 19, 8:54 am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [January 19, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/date-range-comparison/158117/3 "2009-01-19T16:40:09Z")

</div>

David Medinets wrote:

> Sort by date with a limit of one? Hopefully you have an index on that  
> field?
> 
> On Jan 19, 8:54ï¿½am, Sam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

forgot to mention that I’m using thinking sphinx as a search engine my  
statement looks like this

@allRooms = Room.search :conditions =\> {:created\_at =\> here is where I  
should have a statement}

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [January 19, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/date-range-comparison/158117/4 "2009-01-19T21:15:17Z")

</div>

You can do this in ActiveRecord is

Room.find :first, :order =\> ‘created\_at desc’

See  
[http://ts.freelancing-gods.com/rdoc/classes/ThinkingSphinx/Search.html#M000071for](http://ts.freelancing-gods.com/rdoc/classes/ThinkingSphinx/Search.html#M000071for)  
more about ordering search results, which is really what you want.

I don’t think the AR flavor I gave above is likely to work out of the  
box  
with the Sphinx #search method. However, you could probably just ignore  
the  
Sphinx part if you really just want the most recently-created record of  
your  
rooms table, and let AR retrieve it for you.

On Mon, Jan 19, 2009 at 9:40 AM, Sam G.  
\<removed\_email\_address@domain.invalid
