Hi. I have a table with two fields, from_date and to_date. Now I want to do something like this: SELECT * FROM table WHERE from_date < TODAY AND to_date > today; I''m new to Ruby on Rails, so any help is much appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
find(:all, :conditions => ["from_data < ? AND to_date > ?", TODAY, today]) I think you''d better read some tutorial books at first. Agile Web Development With Rails can be a good start. On 9/6/07, Kim G <kimgry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Hi. > > I have a table with two fields, from_date and to_date. Now I want to > do something like this: > > SELECT * FROM table WHERE from_date < TODAY AND to_date > today; > > I''m new to Ruby on Rails, so any help is much appreciated. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Sep 6, 2007, at 10:18 AM, Kim G wrote:> Hi. > > I have a table with two fields, from_date and to_date. Now I want to > do something like this: > > SELECT * FROM table WHERE from_date < TODAY AND to_date > today; > > I''m new to Ruby on Rails, so any help is much appreciated.class Daterange < ActiveRecord::Base # to match your example, not that this should ever be a real name set_table_name ''table'' def self.as_of(date=Time.now) self.find(:all, :conditions => [ ''? BETWEEN from_date AND to_date'', date.to_s(:db) ]) end end Then just call: Daterange.as_of(3.days.from_now) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks to both of you. Rob''s solution was exactly what I was looking for. The SQL and table name was only to illustrate the problem. On Sep 6, 5:21 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 6, 2007, at 10:18 AM, Kim G wrote: > > > Hi. > > > I have a table with two fields, from_date and to_date. Now I want to > > do something like this: > > > SELECT * FROM table WHERE from_date < TODAY AND to_date > today; > > > I''m new to Ruby on Rails, so any help is much appreciated. > > class Daterange < ActiveRecord::Base > # to match your example, not that this should ever be a real name > set_table_name ''table'' > > def self.as_of(date=Time.now) > self.find(:all, > :conditions => [ ''? BETWEEN from_date AND to_date'', > date.to_s(:db) ]) > end > end > > Then just call: > Daterange.as_of(3.days.from_now) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---