[Rails] Best practice for cascading deletes

Scott Barron scott at elitists.net
Tue Dec 14 13:37:14 GMT 2004


On Tue, Dec 14, 2004 at 11:26:36AM -0300, Demetrius Nunes wrote:
> I am using a MySql backend to support a model that has several has_many 
> relationships.
> 
> Yesterday I noticed that Rails is not cascading a delete on a parent 
> object to its children as I thought it would do automatically. (I am not 
> using foreign key constraints, at least for now).
> 
> So, should I implement this cascading using rails/ruby code or is it 
> better to implement this using the database and foreign key constraints?
> 
> By the way, I've been working with "robust" technologies, like Java and 
> .NET for at least 8 years, and I am often amazed with how much I can get 
> from so few lines of code using Ruby on Rails. This is by far the most 
> enjoyable web framework/language combination I've ever used.
> 
> Thanks a lot,
> Demetrius

The has_one and has_many relationships both have a dependent option that
will kill off the object they're linked to.  I believe the habtm
relationship will remove its entries from the join table when it is
destroyed but I don't think there is any implicit destruction of the
objects it is linked to (since they can be linked to other objects in
the table you're destroying an object from).

I don't remember if these are in 0.8.5 or not, I've been using rails
from svn for a while now and some features have blurred versions for me
;)

-Scott


More information about the Rails mailing list