Can I set up Cascade deleting in Rails?

Ruby on-RailsRubyDatabase

Ruby on-Rails Problem Overview


I know this is probably on the Internet somewhere but I can't find the answer here on Stackoverflow so I thought I may boost up the knowledge base here a little.

I'm a newbie to Ruby and Rails but my company is getting pretty invested in it so I'm trying to get to know it in a little more detail.

It's been difficult for me to change my mindset to designing an application from the "model" rather the from the database, so I'm trying to figure out how would do all of the design work that I have classically done in the Database in the Rails model instead.

So the most recent task that I have given myself is to figure out how to configure a Rails database model to do cascading deletes? Is there an easy way of doing this? Or would I have to go into the MySql and set this up?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

you can also set the :dependent option to :delete_all. :delete_all will issue a single SQL statement to delete all child records. because of this using :delete_all may give you better performance.

has_many :memberships, dependent: :delete_all

Solution 2 - Ruby on-Rails

Yeah you can, if you are using a relationship like has_many you just do this

has_many :memberships, dependent: :destroy

Solution 3 - Ruby on-Rails

Contrary to the provided answer I highly suggest also doing this on a database level. In case you have different processes or a multi threaded environment it could happen that records are not properly deleted. Furthermore the database foreign key makes things way faster when deleting lots of data.

Like in the suggested answer do this:

has_many :memberships, dependent: :delete_all

However also make sure to setup a foreign_key in a migration. That way the database takes care of deleting the records automatically for you.

To nullify the values when a membership is deleted, assuming you have a user model:

add_foreign_key :users, :memberships, on_delete: :nullify

You can also delete all the models whenever a membership is deleted

add_foreign_key :users, :memberships, on_delete: :cascade

Solution 4 - Ruby on-Rails

Just keep in mind that delete_all will not execute any callbacks (like before_destroy and after_destroy) on the child records.

Solution 5 - Ruby on-Rails

It looks like this plugin might give you what you're looking for if you want the cascading deletes reflected in the actual database structure:

http://www.redhillonrails.org/foreign_key_migrations.html

Format for using this in a migration would be something like this:

create_table :orders do |t|
  t.column :customer_id, :integer, :on_delete => :set_null, :on_update => :cascade
  ...
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmatt_devView Question on Stackoverflow
Solution 1 - Ruby on-RailsMike BreenView Answer on Stackoverflow
Solution 2 - Ruby on-RailsdanmayerView Answer on Stackoverflow
Solution 3 - Ruby on-RailsHendrikView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJarin UdomView Answer on Stackoverflow
Solution 5 - Ruby on-RailsSean McMainsView Answer on Stackoverflow