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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 > > endThanks James, you hit the nail on the head. -Drew -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---