How to delete data with foreign key

environment

This article uses Ruby 2.6.5 installed on macOS Catalina 10.15.6.

Premise

We have created three models and the situation is as follows.

User / User Report / Report Comment / User-report association table

The following error occurred when deleting a report with comments.

Error occurred

ActiveRecord::InvalidForeignKey in ReportsController#destroy Foreign key error スクリーンショット 2020-10-22 10.00.46.png

Think about the solution to what is the cause.

Cause

When deleting a report, it is determined that the cause is that the user in the comment table and the id value of the report are lost. (Due to foreign key constraints) I want to be able to delete comments if the user and report ids are no longer associated.

solution

dependent: :Describe destroy in User model and Report model.


 With this description, when you delete the parent model (Report), you can also delete the "child model (Comment)" associated with the parent model. "

## Verification


#### **`app/models/user.rb`**
```ruby

class User < ApplicationRecord
  has_many :reports
  has_many :comments, dependent: :destroy
end

app/models/report.rb


class Report < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
end

Completed without incident! that's all. I hope it helps those who have similar problems or are hitting a wall!

reference

I referred to the following, so if you want to understand the contents more deeply, please take a look! https://qiita.com/Ushinji/items/650fa295a3054d2fe582 https://qiita.com/ITmanbow/items/2170ccaceafd5d401df8 https://qiita.com/Tsh-43879562/items/fbc968453a7063776637

Recommended Posts

How to delete data with foreign key
How to use a foreign key with FactoryBot ~ Another solution
Add foreign key to column with migrate
How to rename a model with foreign key constraints in Rails
How to delete BOM (UTF-8)
How to delete a new_record object built with Rails
How to number (number) with html.erb
How to delete untagged images in bulk with Docker
How to update with activerecord-import
[Rails] How to write user_id (foreign key) in strong parameter
How to scroll horizontally with ScrollView
How to get started with slim
How to use Spring Data JDBC
[Rails] Various ways to delete data
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
[Beginner] How to delete NO FILE
How to add the delete function
How to start Camunda with Docker
How to delete only specific data from data created using Form object
How to delete large amounts of data in Rails and concerns
How to encrypt and decrypt with RSA public key in Java
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to get date data in Ruby
How to compile Java with VsCode & Ant
How to delete the wrong migration file
[Java] How to compare with equals method
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
[Java] How to add data to List (add, addAll)
How to switch thumbnail images with JavaScript
How to delete the migration file NO FILE
[Note] How to get started with Rspec
[IOS] How to get data from DynamoDB
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to overwrite Firebase data in Swift
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
[Rails] How to handle data using enum
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to SSH into Ubuntu from a terminal with public key authentication
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to delete a controller etc. using a command
How to use Java framework with AWS Lambda! ??