I have a Post model that has a column ''published_on''. What I would like to do is be able to set the published_on date in the future so a post will automatically become visible to the pubic if the published_on timestamp is before the current timestamp. I have created a named scope for this so I can do something like: @posts = Post.published Here is the named scope in my Post model: named_scope :published, lambda { { :conditions => [''published_on < ?'', Time.now] } } The problem is timezones. I am in the Central timezone (-6). So in my environment.rb I have set config.time_zone = ''Central Time (US & Canada)''. When I use script/console:>> Time.now=> Tue Jan 19 16:17:37 -0600 2010>> p = Post.find(1)=> #<Post id: 2, category_id: nil, title: "This Is The Post Of Tomorrow", content: "This post will automatically be published on 2010-0...", published_on: "2010-01-19 18:20:59", created_at: "2010-01-18 22:54:24", updated_at: "2010-01-19 18:21:01">>> p.published_on = Time.now=> Tue Jan 19 16:18:32 -0600 2010>> p.save=> true>> p.published_on=> Tue, 19 Jan 2010 16:18:32 CST -06:00>> p=> #<Post id: 2, category_id: nil, title: "This Is The Post Of Tomorrow", content: "This post will automatically be published on 2010-0...", published_on: "2010-01-19 22:18:32", created_at: "2010-01-18 22:54:24", updated_at: "2010-01-19 22:18:35"> So p.published on returns a time of 16:18:32 but in the database we see 22:18:32, a difference of six hours (central time). The named scope generates SQL like: SELECT * FROM `posts` WHERE (published_on < ''2010-01-19 16:21:18'') 16:18:32 is less than 16:21:18, but the database is 22:18:32. Is there a setting that I need to modify so that the timestamps are inserted correctly or selected correctly? What am I doing wrong? Thanks! Which is correct. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 19, 2010, at 5:22 PM, jm wrote:> I have a Post model that has a column ''published_on''. What I would > like to do is be able to set the published_on date in the future so a > post will automatically become visible to the pubic if the > published_on timestamp is before the current timestamp. > > I have created a named scope for this so I can do something like: > @posts = Post.published > > Here is the named scope in my Post model: > named_scope :published, lambda { { :conditions => [''published_on < ?'', > Time.now] } }Here use Time.now.utc and see if that doesn''t solve your problem. -Rob> > The problem is timezones. I am in the Central timezone (-6). So in my > environment.rb I have set config.time_zone = ''Central Time (US & > Canada)''. > > When I use script/console: > >>> Time.now > => Tue Jan 19 16:17:37 -0600 2010 > >>> p = Post.find(1) > => #<Post id: 2, category_id: nil, title: "This Is The Post Of > Tomorrow", content: "This post will automatically be published on > 2010-0...", published_on: "2010-01-19 18:20:59", created_at: > "2010-01-18 22:54:24", updated_at: "2010-01-19 18:21:01"> >>> p.published_on = Time.now > => Tue Jan 19 16:18:32 -0600 2010 >>> p.save > => true >>> p.published_on > => Tue, 19 Jan 2010 16:18:32 CST -06:00 >>> p > => #<Post id: 2, category_id: nil, title: "This Is The Post Of > Tomorrow", content: "This post will automatically be published on > 2010-0...", published_on: "2010-01-19 22:18:32", created_at: > "2010-01-18 22:54:24", updated_at: "2010-01-19 22:18:35"> > > So p.published on returns a time of 16:18:32 but in the database we > see 22:18:32, a difference of six hours (central time). > > The named scope generates SQL like: > SELECT * FROM `posts` WHERE (published_on < ''2010-01-19 16:21:18'') > > 16:18:32 is less than 16:21:18, but the database is 22:18:32. > > Is there a setting that I need to modify so that the timestamps are > inserted correctly or selected correctly? What am I doing wrong? > Thanks! > > Which is correct. > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 > . > >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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That worked perfectly! Thank you! On Jan 19, 7:42 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jan 19, 2010, at 5:22 PM, jm wrote: > > > I have a Post model that has a column ''published_on''. What I would > > like to do is be able to set the published_on date in the future so a > > post will automatically become visible to the pubic if the > > published_on timestamp is before the current timestamp. > > > I have created a named scope for this so I can do something like: > > @posts = Post.published > > > Here is the named scope in my Post model: > > named_scope :published, lambda { { :conditions => [''published_on < ?'', > > Time.now] } } > > Here use Time.now.utc and see if that doesn''t solve your problem. > > -Rob > > > > > > > > > The problem is timezones. I am in the Central timezone (-6). So in my > > environment.rb I have set config.time_zone = ''Central Time (US & > > Canada)''. > > > When I use script/console: > > >>> Time.now > > => Tue Jan 19 16:17:37 -0600 2010 > > >>> p = Post.find(1) > > => #<Post id: 2, category_id: nil, title: "This Is The Post Of > > Tomorrow", content: "This post will automatically be published on > > 2010-0...", published_on: "2010-01-19 18:20:59", created_at: > > "2010-01-18 22:54:24", updated_at: "2010-01-19 18:21:01"> > >>> p.published_on = Time.now > > => Tue Jan 19 16:18:32 -0600 2010 > >>> p.save > > => true > >>> p.published_on > > => Tue, 19 Jan 2010 16:18:32 CST -06:00 > >>> p > > => #<Post id: 2, category_id: nil, title: "This Is The Post Of > > Tomorrow", content: "This post will automatically be published on > > 2010-0...", published_on: "2010-01-19 22:18:32", created_at: > > "2010-01-18 22:54:24", updated_at: "2010-01-19 22:18:35"> > > > So p.published on returns a time of 16:18:32 but in the database we > > see 22:18:32, a difference of six hours (central time). > > > The named scope generates SQL like: > > SELECT * FROM `posts` WHERE (published_on < ''2010-01-19 16:21:18'') > > > 16:18:32 is less than 16:21:18, but the database is 22:18:32. > > > Is there a setting that I need to modify so that the timestamps are > > inserted correctly or selected correctly? What am I doing wrong? > > Thanks! > > > Which is correct. > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > . > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en > > . > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.