Forum: Ruby on Rails ActiveRecord find upcoming events

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Drew O. (Guest)
on 2007-02-27 20:07
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
James S. (Guest)
on 2007-02-27 20:17
(Received via mailing list)
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/
Drew O. (Guest)
on 2007-02-27 20:29
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
This topic is locked and can not be replied to.