[Rails] Best practice for cascading deletes
John Wilger
johnwilger at gmail.com
Tue Dec 14 14:08:50 GMT 2004
On Tue, 14 Dec 2004 08:37:14 -0500, Scott Barron <scott at elitists.net> wrote:
> On Tue, Dec 14, 2004 at 11:26:36AM -0300, Demetrius Nunes wrote:
> > So, should I implement this cascading using rails/ruby code or is it
> > better to implement this using the database and foreign key constraints?
Perhaps in violation of DRY ( ;-) ), I tend to do it in _both_ places.
This way, the application will behave properly if used with a database
that doesn't support foreign-key constraints (MySQL will silently
create your tables as MyISAM if InnoDB or Berkely tables aren't
available), and the database will (assuming the foreign-key
constraints are available) enforce the relationships properly if some
other application ends up using it as well.
The need/desire to do this obviously depends on the nature of your project.
> 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
> ;)
They are in 0.8.5. (Although I do some work with the devleopment
versions of Rails, I only integrate the official releases into my
projects.)
--
Regards,
John Wilger
-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
More information about the Rails
mailing list