Skip to content
accelerando

Turn off RoRs automatic timezone conversion for columns

I couldn’t find this in the documents, but Geoff Buesing showed me the hooks to turn off Ruby On Rails’ automatic timezone conversions for some columns of a model or a complete model:

# Turn it off for just some columns
class Picture < ActiveRecord::Base
 def self.skip_time_zone_conversion_for_attributes
   [:created_at, :published_at]
 end
end
 
# Turin it off for the whole model
class Picture < ActiveRecord::Base
 def self.time_zone_aware_attributes
   false
 end
end

Thanks a lot!

5 Comments

  1. ashik ali wrote:

    Hi ,
    Actually I am developing rails front end application to integrate back end application via database . Not standalone Rails applications development.

    Highly appreciation for the rails developer to get the timezone as default also very easy to configure timezone .

    But when we talk about integrated application as like me that is java and rails both application access the same database where the situation
    I don’t like time conversion according to different timezones .

    So really it is very useful for me to integrate rails with java..etc without timezone conflict .

    Posted on 08-Apr-10 at 9:38 am | Permalink
  2. matt wrote:

    I was looking for the answer to that question all day. It works great. Thanks a lot !

    Posted on 18-Sep-10 at 9:05 pm | Permalink
  3. Derek wrote:

    Thank you. For a schedule of events for a physical business, time zone is not important. This helped us greatly. Much appreciated.

    Posted on 24-Feb-12 at 5:00 pm | Permalink
  4. josh schramm wrote:

    Thank you for this. Been trying to figure out this issue all day and finally stumbled on this post.

    Posted on 08-Jan-13 at 8:59 pm | Permalink
  5. Michael wrote:

    You’re welcome Josh! Does this still work in newer Rails versions? Nice!

    Posted on 09-Jan-13 at 9:00 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

*