Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade delete in Ruby ActiveRecord models?

I was following the screencast on rubyonrails.org (creating the blog).

I have following models:

comment.rb

class Comment < ActiveRecord::Base     belongs_to :post     validates_presence_of :body # I added this end 

post.rb

class Post < ActiveRecord::Base     validates_presence_of :body, :title     has_many :comments end 

Relations between models work fine, except for one thing - when I delete a post record, I'd expect RoR to delete all related comment records. I understand that ActiveRecords is database independent, so there's no built-in way to create foreign key, relations, ON DELETE, ON UPDATE statements. So, is there any way to accomplish this (maybe RoR itself could take care of deleting related comments? )?

like image 867
Jakub Lédl Avatar asked Dec 13 '09 15:12

Jakub Lédl


People also ask

What is cascade delete?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

What is the effect when you enable the cascade delete related records?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.

What is Ruby ActiveRecord?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

Yes. On a Rails' model association you can specify the :dependent option, which can take one of the following three forms:

  • :destroy/:destroy_all The associated objects are destroyed alongside this object by calling their destroy method
  • :delete/:delete_all All associated objects are destroyed immediately without calling their :destroy method
  • :nullify All associated objects' foreign keys are set to NULL without calling their save callbacks

Note that the :dependent option is ignored if you have a :has_many X, :through => Y association set up.

So for your example you might choose to have a post delete all its associated comments when the post itself is deleted, without calling each comment's destroy method. That would look like this:

class Post < ActiveRecord::Base   validates_presence_of :body, :title   has_many :comments, :dependent => :delete_all end 

Update for Rails 4:

In Rails 4, you should use :destroy instead of :destroy_all.

If you use :destroy_all, you'll get the exception:

The :dependent option must be one of [:destroy, :delete_all, :nullify, :restrict_with_error, :restrict_with_exception]

like image 173
John Topley Avatar answered Sep 27 '22 22:09

John Topley