Implementation of delete function (if you have foreign_key)

Overview

When deleting a model with a foreign key in Rails, I had a hard time implementing it by simply writing the routing, controller and view file, so I will output it.

Implementation details

Suppose you want to delete a model called item that has item_purchase that links data about product purchases as a foreign key.

This item_purchase has a user_id who is the purchaser and an item_id which is the purchased item.

Here, if you simply delete the item, there will be a record in item_purchase where only item_id is missing.

Therefore, when deleting an item, we will describe it so that the related item_purchase record itself can be deleted.

Implementation

First, let's add an additional description to the model association.

models/item.rb


has_one :item_purchase, foreign_key: :item_id, dependent: :destroy

models/item_purchase.rb


belongs_to :item, optional: true

I won't introduce it here, but since the user should also be linked, don't forget to describe that as well.

After that, you can define the destroy method in the controller as usual.

Recommended Posts

Implementation of delete function (if you have foreign_key)
Implementation of search function
Implementation of pagination function
Implementation of sequential search function
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
Implementation of image preview function
[Rails] Implementation of category function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implementation of like function in Java
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
Implementation of user authentication function using devise (3)
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
Do you need a memory-aware implementation of Java?
[Rails] Implementation of retweet function in SNS application
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Implementation of search function Learning memo (portfolio creation)
We have collected various implementation methods of singleton.
DM function implementation
If you make a mistake in the description location of Gem, delete Gemfile.lock once.
Implementation of GKAccessPoint
Ruby on Rails <2021> Implementation of simple login function (form_with)
If you get tired of drawing an ER diagram
[Rails] Implementation of drag and drop function (with effect)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function