Search code examples
ruby-on-railsruby-on-rails-4activerecordrails-migrations

Are Rails create table migrations supposed to have created_at and updated_at?


In my experience, Rails g migration create table migrations always added timestamps to the table. But that is not happening:

rails g migration CreateFoo bar:references

class Foo < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.references :bar, index: true
    end
  end
end

Is this expected behavior? Did this change in Rails 4?

ActiveRecord 4.1.6


Solution

  • timestamps are only created with generating models.

    here's an example from the guide.

    http://guides.rubyonrails.org/migrations.html

    $ bin/rails generate migration CreateProducts name:string part_number:string
    

    generates

    class CreateProducts < ActiveRecord::Migration
      def change
        create_table :products do |t|
          t.string :name
          t.string :part_number
        end
      end
    end
    

    up

    You can make a suggestion by looking at provided code examples there.

    Also look at:

    rails g migration --help
    rails g model --help
    

    and you will see that timestamps are only provided automatically with "g model" (and you can actually disable it for models)