So I have three tables, table_a, table_c, and table_u. both table_c and table_u reference table_a so basically these three tables are related If I delete an entry in table_a, I want all entries in table_c and table_u that reference table_a deleted as well. Is there a way to do this? What does the code look like to do something like this? Thanks! Gilles -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I think you just want something like: has_many :things, :dependent => :destroy in your model. On Jan 9, 9:16 am, Gilles <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> So I have three tables, table_a, table_c, and table_u. > > both table_c and table_u reference table_a > > so basically these three tables are related > > If I delete an entry in table_a, I want all entries in table_c and > table_u that reference table_a deleted as well. > > Is there a way to do this? What does the code look like to do something > like this? > > Thanks! > > Gilles > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christos Zisopoulos
2007-Jan-08 23:52 UTC
Re: deleting all related tuples in all relevant tables
I am assuming that you also have the three classes that correspond to the tables set up with a has_many relationship... So, def Table_a < ActiveRecord::Base has_many :table_as, :dependent => :destroy has_many :table_us, dependent => :destroy end def Table_b < ActiveRecord::Base belongs_to :table_a end def Table_u < ActiveRecord::Base belongs_to :table_a end The :dependent => :destroy is all you need in order to be able to do... Table_a.find(:first).destroy ...and have all Table_u and Table_c entries that reference the first entry in Table_a destroyed. Have a look at the options here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ ClassMethods.html#M000530 -christos On 9 Jan 2007, at 00:16, Gilles wrote:> > So I have three tables, table_a, table_c, and table_u. > > both table_c and table_u reference table_a > > so basically these three tables are related > > If I delete an entry in table_a, I want all entries in table_c and > table_u that reference table_a deleted as well. > > Is there a way to do this? What does the code look like to do > something > like this? > > Thanks! > > Gilles > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---