[Rails] What to do when you want to generate an error that cannot be destory when foreign key restrictions are applied

Introduction

I want to raise an exception or error when the associated record exists when deleting a record. This is a situation where the parent element should not be erased if it is used in a foreign key.

When I try to delete it, a Ruby error screen appears.

Cause

It was because there was no definition of the behavior for the model associated with Model. The migration file foreign key: True was not enough.

Countermeasures

It was solved by writing the dependent option in the Model. Write the following for the model.

#Model called contract
class Contract < ApplicationRecord
  has_many :...., dependent: :restrict_with_error

end

By writing dependent:: restrict_with_error after the association description, it was possible to display the error when destory cannot be done. The following conditions have been met and resolved! ..


#Controller of model called contract
def destroy
    contract = Contract.find(params[:id])
    if contract.destroy
      redirect_to contract_path, notice: "Deleted the selected contract"
    else
      redirect_to contract_path, alert: "Cannot be deleted because it is in use"
    end
end

reference

・ Https://dorarep.page/articles/rails-dependent#dependent_restrict_with_exception_restrict_with_error

Recommended Posts

[Rails] What to do when you want to generate an error that cannot be destory when foreign key restrictions are applied
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
<Dot installation> What to do if you cannot proceed due to an error when building a development environment for Rails learning.
What to do if you get an error when you hit Heroku logs
What to do when you want to delete a migration file that is "NO FILE"
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
What to do if you get an "A server is already running." Error when you try to start the rails server
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
[Rails] What to do if you can't get an error message with the errors method
What to do if you get an error in Basic authentication during Rails test code
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
What to do when CentOS cannot be started with VirtualBox on Catalina
What to do if you get an uninitialized constant Likes Controller error
What to do if an error occurs when nokogiri enters when bundle install
[Rails] What to do if you get an error saying "Could not find a JavaScript runtime." When executing the rails s command on Catalina
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
What to do if you get an error on heroku rake db: migrate
What to do if ffi installation fails when launching an application in Rails
[Rails / Maybe it can be applied to other things? ] What to do if failure / error __send__ (method file) rspec cannnot road such file appears when executing RSpec
What do you use when converting to String?
[React.useRef] What to do when the latest state cannot be referenced in the event listener
What to do if you get the error Couldn't find Item without an ID
What to do if you can't find your API key after deploying to Rails heroku
What to do if you get a port error when docker-compose up on Mac
I can't deploy! Resolve an error that can't be pushed to heroku (Rails Tutorial Chapter 1)
What to do when an error occurs in rails db: migrate ((StandardError: An error has occurred, this and all later migrations canceled :))
What to do when an error (StandardError: An error has occurred, this and all later migrations canceled:) appears in rails db: migrate
What to do if rails server can't be stopped
What to do when a null byte error occurs
What to do when rails creates a 〇〇 2.rb file
What to do if an ActionController :: UnknownFormat error occurs
An introductory book to read when you start Rails
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
What to do when Git Repository cannot be displayed in Team Explorer for Eclipse in Azure
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if you get a "302" error in your controller unit test code in Rails
[Swift5] What to do if you want to commit files to github but there are too many
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
What to do if you get an error saying Cannot find a valid baseurl for repo when you use yum -y install docker in a udemy course using CentOS7
# What to do if you accidentally do rails db: migrate: drop
What to do when Cannot apply expression operators to method binding
What to do if the adb command cannot be executed
What to do if you can't use the rails command
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What to do if you go offline due to SSL certificate error when running Jenkins on Mac
Object-oriented design that can be used when you want to return a response in form format
[Ruby] What to do when the error "cannot load such file" appears when executing VS Code debug
What to do if you get a "Mysql2 :: Error: Operand should contain 1 column (s)" error in Rails
Avoid the problem that session cannot be acquired when canary release when upgrading from Rails 4.2.x to 5.0.x
What to do when you want to know the source position where the method is defined in binding.pry
What to do if an SSH key authentication error occurs during automatic deployment to EC2 with Capistrano
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if you get an [An HTTP request took too long to complete.] Error in Docker.
What to do about the "cannot be read or is not a valid ZIP file" error
What to do if an error occurs when doing npm install axios in React + Typescript project