Hi all, I''m building an application that will allow store owners to keep track of the opening and closing times of their shops. For example, a store owner might input that their shop is open Monday, Wednesday, and Friday from 9am to 5pm. To keep track of these store hours, I''m using Store and Opening models, where each store has many openings. The openings table looks like: id day (string -- day of the week, in words) opens_at (time) closes_at (time) It seems that ActiveRecord (2.1.1) insists on appending a bogus date to the time for inserts & queries. For example, for a named scope to allow quick searches for whether a store is opened for a given day & time, I have to use this: named_scope :open_for, lambda { |day, time| { :conditions =>["day = ? AND opens_at <= ? AND closes_at >= ?", day, "2001-00-01 " << time.to_s(:time) << ":00", "2000-01-01 " << time.to_s(:time) << ": 00" ] } } when I would much prefer something like: named_scope :open_for, lambda { |day, time| { :conditions =>["day = ? AND opens_at <= ? AND closes_at >= ?", day, time, time] } } Does anyone know of a way to handle time columns more elegantly? best, Jacob Patton --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ActiveRecord doesn''t have the concept of a time independent from a date, which is what it seems like you need. As far as I know, the Ruby standard library doesn''t either. I would use another data type. It looks like in your example the minute is always 0, so you could store only the hour in the database. If you need minute granularity, you could use a decimal data type and use 9.25 for 9:15am, 13.5 for 1:30 pm, etc. -Dan Manges http://www.dcmanges.com/blog On Sep 19, 2:33 pm, Jacob Patton <jacob.pat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > I''m building an application that will allow store owners to keep track > of the opening and closing times of their shops. For example, a store > owner might input that their shop is open Monday, Wednesday, and > Friday from 9am to 5pm. > > To keep track of these store hours, I''m using Store and Opening > models, where each store has many openings. > > The openings table looks like: > id > day (string -- day of the week, in words) > opens_at (time) > closes_at (time) > > It seems that ActiveRecord (2.1.1) insists on appending a bogus date > to the time for inserts & queries. For example, for a named scope to > allow quick searches for whether a store is opened for a given day & > time, I have to use this: > > named_scope :open_for, lambda { |day, time| { :conditions =>["day > = ? AND opens_at <= ? AND closes_at >= ?", day, "2001-00-01 " << > time.to_s(:time) << ":00", "2000-01-01 " << time.to_s(:time) << ": > 00" ] } } > > when I would much prefer something like: > > named_scope :open_for, lambda { |day, time| { :conditions =>["day > = ? AND opens_at <= ? AND closes_at >= ?", day, time, time] } } > > Does anyone know of a way to handle time columns more elegantly? > > best, > > Jacob Patton--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I don''t know about Ruby not having a Time object independent from DateTime, as migrations allow for both types of fields in a database and they behave differently. I had a problem similar to yours where Rails insisted in updating Time fields with 1st Januray 2000 and it turned out that I was trying to input a Date Time value in a Time field (or something similar). Try changing the type of the database field and see what happens. On Sep 21, 7:32 am, Dan Manges <daniel.man...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ActiveRecord doesn''t have the concept of a time independent from a > date, which is what it seems like you need. As far as I know, the Ruby > standard library doesn''t either. I would use another data type. It > looks like in your example the minute is always 0, so you could store > only the hour in the database. If you need minute granularity, you > could use a decimal data type and use 9.25 for 9:15am, 13.5 for 1:30 > pm, etc. > > -Dan Mangeshttp://www.dcmanges.com/blog > > On Sep 19, 2:33 pm, Jacob Patton <jacob.pat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all, > > > I''m building an application that will allow store owners to keep track > > of the opening and closing times of their shops. For example, a store > > owner might input that their shop is open Monday, Wednesday, and > > Friday from 9am to 5pm. > > > To keep track of these store hours, I''m using Store and Opening > > models, where each store has many openings. > > > The openings table looks like: > > id > > day (string -- day of the week, in words) > > opens_at (time) > > closes_at (time) > > > It seems that ActiveRecord (2.1.1) insists on appending a bogus date > > to the time for inserts & queries. For example, for a named scope to > > allow quick searches for whether a store is opened for a given day & > > time, I have to use this: > > > named_scope :open_for, lambda { |day, time| { :conditions =>["day > > = ? AND opens_at <= ? AND closes_at >= ?", day, "2001-00-01 " << > > time.to_s(:time) << ":00", "2000-01-01 " << time.to_s(:time) << ": > > 00" ] } } > > > when I would much prefer something like: > > > named_scope :open_for, lambda { |day, time| { :conditions =>["day > > = ? AND opens_at <= ? AND closes_at >= ?", day, time, time] } } > > > Does anyone know of a way to handle time columns more elegantly? > > > best, > > > Jacob Patton--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 21 Sep 2008, at 10:45, Abel wrote:> > I don''t know about Ruby not having a Time object independent from > DateTime, as migrations allow for both types of fields in a database > and they behave differently. >Ruby doesn''t have a pure time of day class. If you''re database have time columns then what you get back is a Time object for 1st January 2000 and the appropriate time. Fred> I had a problem similar to yours where Rails insisted in updating Time > fields with 1st Januray 2000 and it turned out that I was trying to > input a Date Time value in a Time field (or something similar). > > Try changing the type of the database field and see what happens. > > On Sep 21, 7:32 am, Dan Manges <daniel.man...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> ActiveRecord doesn''t have the concept of a time independent from a >> date, which is what it seems like you need. As far as I know, the >> Ruby >> standard library doesn''t either. I would use another data type. It >> looks like in your example the minute is always 0, so you could store >> only the hour in the database. If you need minute granularity, you >> could use a decimal data type and use 9.25 for 9:15am, 13.5 for 1:30 >> pm, etc. >> >> -Dan Mangeshttp://www.dcmanges.com/blog >> >> On Sep 19, 2:33 pm, Jacob Patton <jacob.pat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> Hi all, >> >>> I''m building an application that will allow store owners to keep >>> track >>> of the opening and closing times of their shops. For example, a >>> store >>> owner might input that their shop is open Monday, Wednesday, and >>> Friday from 9am to 5pm. >> >>> To keep track of these store hours, I''m using Store and Opening >>> models, where each store has many openings. >> >>> The openings table looks like: >>> id >>> day (string -- day of the week, in words) >>> opens_at (time) >>> closes_at (time) >> >>> It seems that ActiveRecord (2.1.1) insists on appending a bogus date >>> to the time for inserts & queries. For example, for a named scope >>> to >>> allow quick searches for whether a store is opened for a given day & >>> time, I have to use this: >> >>> named_scope :open_for, lambda { |day, time| { :conditions =>["day >>> = ? AND opens_at <= ? AND closes_at >= ?", day, "2001-00-01 " << >>> time.to_s(:time) << ":00", "2000-01-01 " << time.to_s(:time) << ": >>> 00" ] } } >> >>> when I would much prefer something like: >> >>> named_scope :open_for, lambda { |day, time| { :conditions =>["day >>> = ? AND opens_at <= ? AND closes_at >= ?", day, time, time] } } >> >>> Does anyone know of a way to handle time columns more elegantly? >> >>> best, >> >>> Jacob Patton > >--~--~---------~--~----~------------~-------~--~----~ 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 21, 2:17 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ruby doesn''t have a pure time of day class. If you''re database have > time columns then what you get back is a Time object for 1st January > 2000 and the appropriate time.Yes, that''s what it looks like from my end. I ultimately decided to leave in the date + time concatenation in my named scopes to handle databases with & without time columns. (Sorry for the late post -- I didn''t have a chance to post back before going on vacation.) Jacob --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---