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.

dependent: :destroyオプション

Last updated at Posted at 2021-05-15

※プログラミング学習中の私がアップロードしてます。理解が浅いです。
この記事は厳密な仕様に関するものではなく、考え方理解のまとめとして受け取ってください。

railsガイド
https://railsguides.jp/association_basics.html

Active Recordの関連付け機能を使うと、2つのモデルの間につながりがあることを明示的にRailsに対して宣言でき、それによってモデルの操作を一貫させることができます。著者と書籍の設定するコードを次のように書き直せます。

class Author < ApplicationRecord
  has_many :books, dependent: :destroy
end

class Book < ApplicationRecord
  belongs_to :author
end

(例)自分のコード

(変更前)post.rb
class Post < ApplicationRecord
  belongs_to :user
(変更前)user.rb
class User < ApplicationRecord
  has_one :card
(変更後)user.rb
class User < ApplicationRecord
  has_one :card, dependent: :destroy

*親に設定

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?