0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Railsで誕生日の初期値の書式の書き方がわからなかったので生成してみた。

Last updated at Posted at 2021-04-04

Railsでモデルのカラムがdate型の場合の初期値の書き方が分からなかったのでrails db:migrateで自動生成してみる。
まず、
rails generate model クラス名
等でモデルを作った後
マイグレーションファイルをいじった。
スクリーンショット 2021-04-04 14.43.15.png
t.date :birthとなっているところに注目。

初期値が今から20年前の誕生日ということになる。
これをrails db:migrate:resetで初っ端からマイグレーションファイルを読み込み直すと
下記のような設定値になる。
下記はdbフォルダの下のschema.rbファイルの内容
スクリーンショット 2021-04-04 14.43.29.png

初期値が20年前になっている。これで正しいdate型の初期値の書き方が分かった。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?