Giancarlo Corzo
Giancarlo Corzo

Reputation: 2006

Rails 3 not saving time part in datetimes fields

I notice that my rails 3 app is saving the datetime field without the time part only the date. I working with mysql.

My migration is like this:

class CreateCampaigns < ActiveRecord::Migration
   def self.up
   create_table :campaigns do |t|
      t.string :name
      t.string :subject
      t.string :from_name
      t.string :from_email
      t.string :reply_to
      t.string :location
      t.datetime :send_in_date_time
      t.string :message
      t.string :test_email
      t.boolean :send_now
      t.timestamps
    end
  end

  def self.down
    drop_table :campaigns
  end
end

Let's say I create a new campaign and set the field send_in_date_time

campaign = Campaign.new
campaign.send_in_date_time = Time.now
campaign.save

In the database is store this:

2011-09-20 00:00:00

Also the create_at and modified_at don't store the time. I tested the database along and works fine it store correct the time.

EDIT:

Checking the logs I found that the active record is setting the date without the time. Active Record don't support well datetime?

UPDATE `campaigns` SET `updated_at` = '2011-06-10', `send_in_date_time` = '2011-09-19' WHERE `campaigns`.`id` = 1

Upvotes: 1

Views: 1203

Answers (1)

Giancarlo Corzo
Giancarlo Corzo

Reputation: 2006

I found the solution.

The problem was that I added in the initializer a old code for date formats. look like this code was generating the problem.

file: date_format.rb content:

Time::DATE_FORMATS[:month_and_year] = "%B %Y"
Time::DATE_FORMATS[:note] = lambda { |time| time.strftime("%a, %b %e at %l:%M") + time.strftime("%p").downcase }
Time::DATE_FORMATS[:db] = lambda { |time| time.strftime("%Y-%m-%d")}
Time::DATE_FORMATS[:nice] = lambda { |time| time.strftime("%b %e, %Y")}

I removed this code and everything worked fine

Upvotes: 1

Related Questions