タイムスタンプ ( created_at
& updated_at
) を既存のテーブルに追加する必要があります。次のコードを試しましたが、うまくいきませんでした。
class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_timestamps(:users)
end
end
タイムスタンプ ( created_at
& updated_at
) を既存のテーブルに追加する必要があります。次のコードを試しましたが、うまくいきませんでした。
class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_timestamps(:users)
end
end
タイムスタンプ ヘルパーは、create_table
ブロックでのみ使用できます。これらの列は、列の種類を手動で指定して追加できます。
class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_column :users, :created_at, :datetime, null: false
add_column :users, :updated_at, :datetime, null: false
end
end
これは上で指定したメソッドと同じ簡潔な構文ではありませんがadd_timestamps
、Rails はこれらの列をタイムスタンプ列として扱い、値を通常通り更新します。
移行は、2 つのクラス メソッド (または 3.1 のインスタンス メソッド):up
とdown
(change
場合によっては 3.1 のインスタンス メソッド) です。up
変更をメソッドに入れる必要があります。
class AddTimestampsToUser < ActiveRecord::Migration
def self.up # Or `def up` in 3.1
change_table :users do |t|
t.timestamps
end
end
def self.down # Or `def down` in 3.1
remove_column :users, :created_at
remove_column :users, :updated_at
end
end
3.1 を使用している場合は、以下も使用できますchange
(Dave に感謝):
class AddTimestampsToUser < ActiveRecord::Migration
def change
change_table(:users) { |t| t.timestamps }
end
end
def change
、def change_table
、 と を混同している可能性がありchange_table
ます。
詳細については、移行ガイドを参照してください。
元のコードは右に非常に近く、別のメソッド名を使用する必要があります。Rails 3.1 以降を使用している場合は、change
代わりにメソッドを定義する必要がありchange_table
ます。
class AddTimestampsToUser < ActiveRecord::Migration
def change
add_timestamps(:users)
end
end
古いバージョンを使用している場合は、 の代わりにup
およびdown
メソッドを定義する必要がありchange_table
ます。
class AddTimestampsToUser < ActiveRecord::Migration
def up
add_timestamps(:users)
end
def down
remove_timestamps(:users)
end
end
class AddTimestampsToUser < ActiveRecord::Migration
def change
change_table :users do |t|
t.timestamps
end
end
end
利用可能な変換は
change_table :table do |t|
t.column
t.index
t.timestamps
t.change
t.change_default
t.rename
t.references
t.belongs_to
t.string
t.text
t.integer
t.float
t.decimal
t.datetime
t.timestamp
t.time
t.date
t.binary
t.boolean
t.remove
t.remove_references
t.remove_belongs_to
t.remove_index
t.remove_timestamps
end
http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html
ここには多くの回答がありますが、以前の回答はどれも実際にはうまくいかなかったので、私も投稿します:)
一部の人が指摘しているように、#add_timestamps
残念ながらnull: false
制限が追加されています。これにより、これらの値が入力されていないため、古い行が無効になります。ここでのほとんどの回答は、デフォルト値 ( ) を設定することを示唆していますTime.zone.now
が、古いデータのデフォルトのタイムスタンプは正しくないため、そうしたくありません。テーブルに間違ったデータを追加することに価値がありません。
したがって、私の移行は単純でした:
class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_column :projects, :created_at, :datetime
add_column :projects, :updated_at, :datetime
end
end
いいえnull: false
、その他の制限はありません。created_at
古い行は、 as NULL
、およびupdate_at
asで引き続き有効ですNULL
(行に対して何らかの更新が実行されるまで)。新しい行にはcreated_at
、updated_at
期待どおりにデータが取り込まれます。
(既存のデータベースがあると仮定して)各テーブルにcreated_atフィールドとupdated_atフィールドを追加するために呼び出すことができる単純な関数を作成しました。
# add created_at and updated_at to each table found.
def add_datetime
tables = ActiveRecord::Base.connection.tables
tables.each do |t|
ActiveRecord::Base.connection.add_timestamps t
end
end
これは、既存のテーブルにタイムスタンプを追加する簡単なものです。
class AddTimeStampToCustomFieldMeatadata < ActiveRecord::Migration
def change
add_timestamps :custom_field_metadata
end
end