I went to a programming school for about 3 months and this time I will write what I learned at that time! !!
This time, when creating a blog application, I will write a method to delete user and delete the associated tweet at the same time.
dependent: :destory dependent :: destory is a function that deletes the tweet associated with the user when it is deleted, and thanks to this, you can avoid the fact that only the tweet remains even if you delete the user: point_up_tone2:
user.rb
Class User < ApplicationRecord
has_many :tweets, dependent: :destroy
end
tweet.rb
Class Tweet < ApplicationRecord
belongs_to :user
end
The tweet associated with deleting user in the above description is also deleted.
Please note that if you write dependent :: destroy on the tweet model side, the associated user will be deleted when you delete the tweet! !!
Thank you for watching till the end! !!
I hope you found this article helpful: pray_tone2:
Recommended Posts