Re: [Rails-core] Disable Time Zone Conversion for Specified Column

212 views
Skip to first unread message

Godfrey Chan

unread,
Oct 18, 2012, 8:49:03 PM10/18/12
to rubyonra...@googlegroups.com
Is it possible to offset the "damage" from rails by writing your own attr reader/writer?

Sent from my phone

On 2012-10-18, at 4:39 PM, Karl Smith <threa...@gmail.com> wrote:

Pretty simple, I need to disable time zone conversion for specific columns. I will handle any TZ conversion manually, but I need Rails 3 to forego conversion in both writing and reading, and any AREL functions. But, I don't want to disable the conversion for non-specified attributes.

I know how to disable it for reading:

self.skip_time_zone_conversion_for_attributes = [:test_timestamp]

But this only works for reading. When writing the attribute, it still converts to UTC (yes, I tested this in 3.2.8).

How?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/9JjYyc88WlsJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.

Karl Smith

unread,
Oct 19, 2012, 12:54:44 PM10/19/12
to rubyonra...@googlegroups.com
Tried that. Does not work. Rails still does the time zone conversion when persisting to the database.

Example:

class Widget < ActiveRecord::Base
  def closed_at=(time)
    write_attribute(:closed_at, time)
  end
end

in console:
> Time.zone = "Arizona"
=> "Arizona"
> Time.now.in_time_zone("Hawaii")
=> Fri, 19 Oct 2012 06:43:39 HST -10:00 
> a = Widget.create({closed_at: Time.now.in_time_zone("Hawaii")})
> a.inspect
=> "#<Widget id: 1, closed_at: "2012-10-19 16:43:48">"

Rails is still doing the time zone conversion.

Maybe there is a different way to override the attribute writer?
Reply all
Reply to author
Forward
0 new messages