views:

73

answers:

1

Is there a way to get Rails to save records to the database without it automagically converting the timestamp into UTC before saving? The problem is that I have a few models that pull data from a legacy database that saves everything in Mountain Time and occasionally I have to have my Rails app write to that database. The problem is that every time it does, it converts the time I give it from Mountain Time to UTC, which is 6-7 hours ahead (depending on DST)! Needless to say, this really messes with reporting on that database.

If I could get around doing this, I would. Unfortunately, I can't do anything about the fact that this other database uses a different timezone, nor can I really get away from the need for this app to save to that database occasionally. If I could just get Rails to stop trying to help me, it'd be great.

A: 

In case anyone's curious, what I finally got to work was to do the following:

Assume a class Foo. I created an after_save method to grab the time and then do a manual query against the database, so my solution looks something like this:

class Foo < ActiveRecord::Base
  #stuff relating to class
  def after_save
    if (@changed_attributes.has_key?('CreateStamp'))
      time_str = self.CreateStamp.in_time_zone('Mountain Time (US & Canada)').to_datetime.to_s(:db)
      qry = "UPDATE Lead SET CreateStamp='#{time_str}' WHERE id='#{id}'"
      connection.execute(qry)
    end
  end
end

This way, any time a record was saved to the database and the timestamp field was modified (CreateStamp in my case), I run a second query after the initial save that sends the timestamp in the format I need it to be in and Rails doesn't get in the way.

Shaun