Sorting records into groups by date

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?

Elliot,

Sounds like a good candidate for :named_scope (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. [email protected]

On 27 Aug 2008, at 19:11, Elliot C. <[email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs