How to use dependent :: destroy

background

I think dependent :: destory is essential for app production. I will also output it for my own review.

dependent: What is: destory? ??

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.

How to use

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

How to use dependent :: destroy
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
How to use java class
How to use Swift UIScrollView
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use rails join
Ruby: How to use cookies
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
[Rails] How to use flash messages
How to use EventBus3 and ThreadMode