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.
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-02-27 19: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
4715ae8a9ac5152600ccd3012267ab6b?d=identicon&s=25 James Stewart (Guest)
on 2007-02-27 19:17
(Received via mailing list)
On Feb 27, 2007, at 1:07 PM, Drew Olson 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 Stewart
Play: http://james.anthropiccollective.org
Work: http://jystewart.net/process/
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-02-27 19:29
James Stewart 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.