running through the cookbook tutorial i notice deletion of a category creates a deletion anomaly in the recipes table what is the preferred method of maintaing ri in rails apps? the api has support for groking relationships but does not enforce them - are most people simply addressing this using creat table parents( id auto_increment, name text, primary key (id) ); creat table children( id auto_increment, parent_id int(6), name text, primary key (id), foreign key (parent_id) reference parent (id) on delete cascade ); or addressing it on the rails level with #delete? something like class Parent < ActiveRecord::Base def delete @parent = Parent::find @params[''id''] pid = @parent.id # cascade @children = Children::find_all.select{|c| c.parent_id == pid} @children.each do |child| # delete the child tuple end end end etc. this would, of course, be the *only* way to deal with deletion in a db not supporting true ri like sqlite or certain mysql table types. are there any plans to address this or is it considered (accurately) to be the domain of the rdbms? cheers. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
Ara.T.Howard wrote:> > etc. this would, of course, be the *only* way to deal with deletion in > a db > not supporting true ri like sqlite or certain mysql table types. are there > any plans to address this or is it considered (accurately) to be the > domain of > the rdbms? > > cheers. > > -aYou can do this with the dependent argument to the relationships. eg class Parent < ActiveRecord::Base has_many :childen, :dependent => true end And thus when a parent is destroyed, it''s children vanish too. If you want to do something else, like set children''s FK to null in the case that that''s allowed, you just create a before_destroy callback in the Parent model. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tue, 15 Feb 2005, Scott Barron wrote:>> etc. this would, of course, be the *only* way to deal with deletion in a >> db not supporting true ri like sqlite or certain mysql table types. are >> there any plans to address this or is it considered (accurately) to be the >> domain of the rdbms? >> >> cheers. >> >> -a > > You can do this with the dependent argument to the relationships. > > eg > > class Parent < ActiveRecord::Base > has_many :childen, :dependent => true > end > > And thus when a parent is destroyed, it''s children vanish too. If you want > to do something else, like set children''s FK to null in the case that that''s > allowed, you just create a before_destroy callback in the Parent model. > > -Scotti was impressed - now i''m *very* impressed! does anyone know if this handles circular and/or deep relationships? still whistling to myself... -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
Ara.T.Howard wrote:> On Tue, 15 Feb 2005, Scott Barron wrote: > >>> etc. this would, of course, be the *only* way to deal with deletion >>> in a >>> db not supporting true ri like sqlite or certain mysql table types. are >>> there any plans to address this or is it considered (accurately) to >>> be the >>> domain of the rdbms? >>> >>> cheers. >>> >>> -a >> >> >> You can do this with the dependent argument to the relationships. >> >> eg >> >> class Parent < ActiveRecord::Base >> has_many :childen, :dependent => true >> end >> >> And thus when a parent is destroyed, it''s children vanish too. If you >> want >> to do something else, like set children''s FK to null in the case that >> that''s >> allowed, you just create a before_destroy callback in the Parent model. >> >> -Scott > > > i was impressed - now i''m *very* impressed! > > does anyone know if this handles circular and/or deep relationships? > > still whistling to myself... > > -aI have used it in a chain of dependencies with success. I think I once had accidentally set up a circular chain of dependents and it blew up because an object is frozen after it is destroyed, so it may not work there. Then again, it could have been something else causing my problems because I wasn''t intentionally setting up circular relationships. But deep relationships do work. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails