ActiveRecord find upcoming events


#1

All -

I have an event model which has a date attribute. What I would like to
do is add a class method to Event that tells me the next 10 upcoming
events such that they are ordered by date (closest to today first) and
that only events which have not yet occurred will be displayed. How
would I do this using find?

Thanks,
Drew


#2

On Feb 27, 2007, at 1:07 PM, Drew O. wrote:

I have an event model which has a date attribute. What I would like to
do is add a class method to Event that tells me the next 10 upcoming
events such that they are ordered by date (closest to today first) and
that only events which have not yet occurred will be displayed. How
would I do this using find?

Something like the following should work:

class Event < ActiveRecord::Base

def next_ten(after = DateTime.now, limit = 10)
find(:all, :conditions => [‘date > ?’, after], :limit =>
limit, :order => ‘date DESC’)
end

end

James.


James S.
Play: http://james.anthropiccollective.org
Work: http://jystewart.net/process/


#3

James S. wrote:

class Event < ActiveRecord::Base

def next_ten(after = DateTime.now, limit = 10)
find(:all, :conditions => [‘date > ?’, after], :limit =>
limit, :order => ‘date DESC’)
end

end

Thanks James, you hit the nail on the head.

-Drew