coding addicted
coding addicted

Reputation: 3430

Rails generate migration does not set :null => false to created_at and update_at

I created a brand new rails app, generate some scaffolds and some models. After few tests i realized that the columns generated by t.timestamps does not include the "traditional" :null => false option. I need this behaviour, i can't see what is doing this.

I recreate a new app, thinking that's a new rails 4 functionality, but i can't get the save behaviour. In this app the timespamp work as it should.

In the "bugging app" in a rails console when i create a post we can see :

Loading development environment (Rails 4.0.1)

2.0.0-p247 :001 > Post.create(title:"test") 

(0.1ms)  begin transaction SQL (8.6ms)  INSERT INTO "posts" ("created_at", "title", "updated_at") VALUES (?, ?, ?)  [["created_at", Wed, 27 Nov 2013 21:19:01 EST -05:00], ["title", "test"], ["updated_at", Wed, 27 Nov 2013 21:19:01 EST -05:00]]
(0.8ms)  commit transaction
=> #<Post id: 1, title: "test", content: nil, excerpt: nil, dashboard: nil, created_at: "2013-11-28 02:19:01", updated_at: "2013-11-28 02:19:01"> 

2.0.0-p247 :002 > Post.last

Post Load (0.4ms)  SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
=> #<Post id: 1, title: "test", content: nil, excerpt: nil, dashboard: nil, created_at: nil, updated_at: nil> 

The Timestamp is set but not save in the database.

I think i'm missing something. Any idea how to restore the default behaviour?

EDIT: migration file

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.text :content
      t.text :excerpt
      t.boolean :dashboard

      t.timestamps
    end
  end
end

Upvotes: 4

Views: 3787

Answers (1)

coding addicted
coding addicted

Reputation: 3430

Finally I found the error:

After few attempts i check everything in the config dir. The only possibility i saw for a timestamp was the time zone configuration.

After commenting this (config/application.rb):

#config.active_record.default_timezone = 'Eastern Time (US & Canada)'

Everything was back to normal! No more :null => false, but timestamp was able to record in the database.

Upvotes: 5

Related Questions