I''m rails a newbie: I have a form setup where users selects datetime, the datetime serves the purpose of allowing the user to specify when the entry expires. In efforts to filter entrys that have an earlier datetime than now I am using the following: def self.find_active find(:all, :conditions => [''expiration > ?'', DateTime.now]) It does not filter out the expiration inputs that are further in advance than DateTime.now Any help would be greatly appreciated :) -- Posted via http://www.ruby-forum.com/.
On Oct 1, 6:27 am, Greg King <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m rails a newbie: > > I have a form setup where users selects datetime, the datetime serves > the purpose of allowing the user to specify when the entry expires. In > efforts to filter entrys that have an earlier datetime than now I am > using the following: > > def self.find_active > find(:all, :conditions => [''expiration > ?'', DateTime.now]) >You''ve got your condition back to front - that returns entries whose expiry is after now Fred> It does not filter out the expiration inputs that are further in advance > than DateTime.now > > Any help would be greatly appreciated :) > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung wrote:> On Oct 1, 6:27�am, Greg King <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> I''m rails a newbie: >> >> I have a form setup where users selects datetime, the datetime serves >> the purpose of allowing the user to specify when the entry expires. �In >> efforts to filter entrys that have an earlier datetime than now I am >> using the following: >> >> def self.find_active >> � � find(:all, :conditions => [''expiration > ?'', DateTime.now]) >> > > You''ve got your condition back to front - that returns entries whose > expiry is after nowI don''t think he''s got it backwards -- he''s looking for entries that haven''t yet expired.> > FredBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
2009/10/1 Greg King <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > I''m rails a newbie: > > I have a form setup where users selects datetime, the datetime serves > the purpose of allowing the user to specify when the entry expires. In > efforts to filter entrys that have an earlier datetime than now I amIt is not completely clear from the above whether you are trying to select records with an expiry date greater than now or earlier than now.> using the following: > > def self.find_active > find(:all, :conditions => [''expiration > ?'', DateTime.now]) > > It does not filter out the expiration inputs that are further in advance > than DateTime.nowWhat type of column (in the database) is expiration? Colin
Rails Newb wrote:> def self.find_active > find(:all, :conditions => [''expiration > ?'', DateTime.now])On a side note: Wouldn''t this be a good candidate for a named_scope rather than a class method? named_scope :active, lamda { :conditions => [ ''expiration > ?'', DateTime.now ] } or named_scope :active, lamda { :conditions => [ ''expiration <= ?'', DateTime.now ] } or whatever conditions you determine are the right ones for your case. P.S. This would be more conventional to the Rails framework: named_scope :active, lamda { :conditions => [ ''expires_at > ?'', DateTime.now ] } where expires_at is a datetime column in the database. -- Posted via http://www.ruby-forum.com/.