[RUBY] How to delete child elements associated with a parent element at the same time

Regarding the delete function of the review model in Rails, it originally worked without problems, but an error occurred after implementing the comment model. I have clarified the cause, so I will share it.

Development environment

Ruby 2.6.5 Rails 6.0.3.3

Implementation details when a problem occurs

Routing is nested with reveiw as the parent and comment as the child.

config/routes.rb


resources :reviews do
    resources :comments, only: [:create, :destroy] 
end

It was implemented so that the review would be deleted. Before the comment was implemented, it could be deleted as it is.

views/reviews/show.html.erb


<%= link_to review_path(id: @review.id), method: :delete do %>
    <div>Delete review</div>
    <div>You can delete the review here.</div>
<% end %>

Modification place

Since we implemented comment as a child element of review, we must implement it so that comment is deleted at the same time as review is deleted. Therefore, modify models / review.rb as follows.

models/review.rb


has_many :comments

↓↓↓

models/review.rb


has_many :comments, dependent: :destroy

The deletion function implementation is completed here.

reference

https://qiita.com/Tsh-43879562/items/fbc968453a7063776637

Recommended Posts

How to delete child elements associated with a parent element at the same time
How to delete the tweet associated with the user when you delete it at the same time
Setting to start multiple units at the same time with Vagrant
Draw a bar graph and a line graph at the same time with MPAndroidChart
How to delete custom Adapter elements using a custom model
[Docker] Delete only the volume associated with a specific container
How to add the same Indexes in a nested array
How to use MinIO with the same function as S3 Use docker-compose
How to call multiple names at once in the same category
How to interact with a server that does not crash the app
How to check before sending a message to the server with Spring Integration
How to compare only the time with Rails (from what time to what time, something like)
How to change the value of a variable at a breakpoint in intelliJ
How to implement the email authentication function at the time of user registration
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
How to get the ID of a user authenticated with Firebase in Swift
How to store data simultaneously in a model associated with a nested form (Rails 6.0.0)
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
How to delete a controller etc. using a command
Set the time of LocalDateTime to a specific time
[Swift] How to link the app with Firebase
How to delete the database when recreating the application
(Java) How to implement equals () for a class with value elements added by inheritance
I tried to create a log reproduction script at the time of apt install
How to save a file with the specified extension under the directory specified in Java to the list
[Docker] How to see the contents of Volumes. Start a container with root privileges.
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
Form and process file and String data at the same time with Spring Boot + Java
How to change the action with multiple submit buttons
How to make a factory with a model with polymorphic association
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
How to run the SpringBoot app as a service
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
How to make a mod for Slay the Spire
How to get started with creating a Rails app
How to enable submit button every time with jquery
[Java] How to start a new line with StringBuilder
I want to add a delete function to the comment function
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
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
Waiting for a JMX connection on 18080 Connecting to multiple Java Application servers over a bastion from a local machine at the same time