6. 2番めのモデルを追加する
app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :article
end
referencesキーワードは、モデルの特殊なデータ型を表す。
指定されたモデル名の後ろに_idを追加した名前を持つ新しいカラムをデーターベーステーブルに作成
db/schema.rb
class CreateComments < ActiveRecord::Migration[6.0]
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :article, null: false, foreign_key: true
t.timestamps
end
end
end
t.references はarticle_idという名前のinteger型のカラムとインデックスとarticlesのidカラムを指す外部キー制約を設定する
dependent: :destroy
削除のオプション