# Sorting records into groups by date

**URL:** <https://www.ruby-forum.com/t/sorting-records-into-groups-by-date/147033>\
**Category:** Rails\
**Created:** [August 27, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/sorting-records-into-groups-by-date/147033 "2008-08-27T20:11:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 27, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/sorting-records-into-groups-by-date/147033/1 "2008-08-27T20:11:56Z")

</div>

I’m trying to wrap my head around this idea, and thought I might be able  
to solicit the help of the community a little bit.

@comments = Finding all comments for a particular thread.

- Each comment has a date of when it was posted.

- I would like to display all comments that were posted in the past 7  
days in one area, and then display all older comments in another.

- I could imagine doing this as two separate database queries, but is  
there a way to modify a for loop to run through them for me?

- How do you handle the date selection to use the current day - 7?

---

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 27, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/sorting-records-into-groups-by-date/147033/2 "2008-08-27T20:28:59Z")

</div>

Elliot,

Sounds like a good candidate for :named\_scope (http://  
[api.rubyonrails.com/classes/ActiveRecord/NamedScope/](http://api.rubyonrails.com/classes/ActiveRecord/NamedScope/)  
ClassMethods.html#M001246)

named\_scope :recent, :conditions =\> [‘created\_at \> ?’,  
7.days.ago.to\_s(:db)]  
named\_scope :older, :conditions =\> [‘created\_at \<= ?’,  
7.days.ago.to\_s(:db)]

@recent\_comments = thread.comments.recent  
@older\_comments = thread.comments.older

As to cutting down to one db call, generally, it won’t hurt to have  
two db calls but maybe someone else has a way.

On Aug 27, 12:11 pm, Elliot C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![elliotchyba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/8dc957/32.png) [@elliotchyba](https://www.ruby-forum.com/u/elliotchyba)\
**Post date:** [August 27, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/sorting-records-into-groups-by-date/147033/3 "2008-08-27T20:38:21Z")

</div>

On 27 Aug 2008, at 19:11, Elliot C. \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> days in one area, and then display all older comments in another.
> 
> - I could imagine doing this as two separate database queries, but is  
> there a way to modify a for loop to run through them for me?

If you’re happy doing it in ruby, the partition function is probably  
what you’re after

Fred
