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, > DemetriusThe 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
On Tue, 14 Dec 2004 08:37:14 -0500, Scott Barron <scott-HDQKq3lYuGDk1uMJSBkQmQ@public.gmane.org> 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
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
Scott Barron wrote:> 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 > ;)There''s a bug with nested has_many relations and dependent in 0.8.5. It appears to be resolved in the soon-to-be 0.9.0. -- Marten Veldthuis