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 1 year has passed since last update.

t.belongs_to()

Posted at
# Adds a reference.
      #
      #  t.references(:user)
      #  t.belongs_to(:supplier, foreign_key: true)
      #  t.belongs_to(:supplier, foreign_key: true, type: :integer)

出典

https://github.com/rails/rails/blob/984c3ef2775781d47efa9f541ce570daa2434a80/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L478:~:text=%23%20Adds%20a%20reference,true%2C%20type%3A%20%3Ainteger)

belongs_toはreferencesのエイリアスなので

add_reference(テーブル名, リファレンス名, オプション引数)

出典

感想

真似してみたらよかった
これで最初から関連付けのついた外部キーの設定ができる

0
0
0

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?