I think dependent :: destory is essential for app production. I will also output it for my own review.
For example, when you create an app like a blog, if you delete a user, the posts associated with that user will also be deleted. Without this description, even if you delete the user, only the post will remain and an error will occur.
user.rb
Class User < ApplicationRecord
has_many :tweets, dependent: :destroy
end
tweet.rb
Class Tweet < ApplicationRecord
belongs_to :user
end
By writing dependent :: destroy on the user model side, all tweets associated with the user will disappear. Note that if you write dependent :: destroy on the tweet model side, the user associated with the tweet will be deleted.
that's all
Recommended Posts