[RUBY] How to delete the tweet associated with the user when you delete it at the same time

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:

Implementation

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.

important point

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

How to delete the tweet associated with the user when you delete it at the same time
How to delete child elements associated with a parent element at the same time
Setting to start multiple units at the same time with Vagrant
How to implement the email authentication function at the time of user registration
How to delete the database when recreating the application
How to set chrony when the time shifts in CentOS7
How to cure the time dilation when launching Japaneseized Kibana
How to add the delete function
How to use MinIO with the same function as S3 Use docker-compose
How to call multiple names at once in the same category
A memo when you want to clear the time part of the calendar
Don't forget to release it when you get the object from S3!
Draw a bar graph and a line graph at the same time with MPAndroidChart
[Rails] How to get the user information currently logged in with devise
How to compare only the time with Rails (from what time to what time, something like)
How to delete the wrong migration file
How to delete the migration file NO FILE
[Java] How to set the Date time to 00:00:00
How to delete data with foreign key
When registering a new user, I got an error called ActiveRecord :: NotNullViolation and how to deal with it.
How to get the query string to actually issue when using PreparedStatement with JDBC
How to get the ID of a user authenticated with Firebase in Swift
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
[Swift] How to link the app with Firebase
[For beginners] How to implement the delete function
When you want to use the method outside
How to insert all at once with MyBatis
How to redirect after user login with Spring-security
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
Behavior noticed when adding RadioButton and initial check at the same time in code
[Rails] How to delete production environment MySQL data after putting it in the development environment
You also need to specify the host when debugging remotely with Java 9 or later
When importing CSV with Rails, it was really easy to use the nkf command
How to deal with `Cannot connect to the Docker daemon at unix: /// var/run/docker.sock. Is the docker daemon running?`
A memo to do for the time being when building CentOS 6 series with VirtualBox
[Android studio / Java] What you don't understand when you touch it for the first time
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
Form and process file and String data at the same time with Spring Boot + Java
Summary of how to use the proxy set in IE when connecting with Java
How to change the action with multiple submit buttons
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
Things to check when it doesn't work with proguard
How to delete a new_record object built with Rails
Organized how to interact with the JDK in stages
How to think when you suddenly understand about generics
How to delete untagged images in bulk with Docker
Completely delete the migration file that you failed to delete
How to enable submit button every time with jquery
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
[Ruby + Rails] When you want to register in Mailchimp's mail list together with user registration
[Java] Method call error when inheritance and interface implementation are performed at the same time
How to write in Model class when you want to save binary data in DB with PlayFramework
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Check how to set the timeout when connecting with Spring + HikariCP + MySQL and executing SQL
Implementation method of linking multiple images to one post and posting at the same time
How to reduce the load on the program even a little when combining characters with JAVA
Whether to make the server side at the time of system rebuild with Kotlin or Java