# Date Range comparison

**URL:** <https://discuss.rubyonrails.org/t/date-range-comparison/38478>\
**Category:** rubyonrails-talk\
**Created:** [January 19, 2009, 1:54pm UTC](https://discuss.rubyonrails.org/t/date-range-comparison/38478 "2009-01-19T13:54:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 19, 2009, 1:54pm UTC](https://discuss.rubyonrails.org/t/date-range-comparison/38478/1 "2009-01-19T13: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:** ![David\_Medinets](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@David\_Medinets](https://discuss.rubyonrails.org/u/David_Medinets)\
**Post date:** [January 19, 2009, 3:13pm UTC](https://discuss.rubyonrails.org/t/date-range-comparison/38478/2 "2009-01-19T15:13:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ryan\_Waldron](https://avatars.discourse-cdn.com/v4/letter/r/a9adbd/32.png) [@Ryan\_Waldron](https://discuss.rubyonrails.org/u/Ryan_Waldron)\
**Post date:** [January 19, 2009, 8:14pm UTC](https://discuss.rubyonrails.org/t/date-range-comparison/38478/3 "2009-01-19T20:14:23Z")

</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#M000071](http://ts.freelancing-gods.com/rdoc/classes/ThinkingSphinx/Search.html#M000071) for 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.
