Ruby Rails - Make Table With created_at Column

t.datetime :created_at, null: false

Just like any other column. Rails will still take care of magic updating because of column name.


Both attempts are okay but contains syntax error.

The problem on the first attempt is that the t. is on datatype not column name.

class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.datetime :created_at, null: false
    end
  end
end

The problem on the second attempt is that remove_column does not work inside create_table block. You can move it outside the block and provide correct syntax to remove the column.

class CreateLinesSources < ActiveRecord::Migration
  def change
    create_table :lines_sources do |t|
      t.timestamps null: false
    end
    remove_column :line_sources, :updated_at, :datetime
  end
end