Search code examples
ruby-on-railsrubypostgresqlgrape-api

Store time in postgresql from ruby


I'm trying to save some attributes of time (Hours:Mitues) in postgresql and I can't understand what happens since INSERT is run until data is saved.

I can see "time:\n- \n-2016-01-21 22:00:00.000000000 +01:00" in my log, but it becomes "21:00:00" in database.

I suppose it's related with +01:00 offset, but I've tried to set config timezone in my app and nothing changed...

My app is an API and it updates this attribute along with many others.

create_table :things do |t|

  t.time :time

end

Here it's updated:

collection.each do |resource|
  resource.update_attributes resource_params[resource.id.to_s].first
end

Solution

  • Yes, you're right. The timezone your data is saved in the db is UTC. The configs you should do in application.rb are:

    config.active_record.default_timezone = :local
    

    and you should declare explicitly your timezone if you haven't yet:

    config.time_zone = 'Madrid'