If I have two models that are in a one-to-one relationship in Ruby on Rails, and I wish to delete a record. How do I make sure I will delete all the related data from both tables? Is there a simple elegant Ruby way to do this? Thank you for your time in advance, Mitch -- 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 -~----------~----~----~----~------~----~------~--~---
On 22 Jul 2008, at 18:43, Mitchell Gould wrote:> > If I have two models that are in a one-to-one relationship in Ruby on > Rails, > and I wish to delete a record. > How do I make sure I will delete all the related data from both > tables? > > Is there a simple elegant Ruby way to do this? >rails provides the :dependant option. If you want a cast iron guarantee then you need to get the database to do it for you (foreign key) Fred> Thank you for your time in advance, > > Mitch > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 22 Jul 2008, at 18:43, Mitchell Gould wrote: > >> >> If I have two models that are in a one-to-one relationship in Ruby on >> Rails, >> and I wish to delete a record. >> How do I make sure I will delete all the related data from both >> tables? >> >> Is there a simple elegant Ruby way to do this? >> > rails provides the :dependant option. > If you want a cast iron guarantee then you need to get the database to > do it for you (foreign key) > > FredThanks Fred. I have a foreign key in one of the tables. Does that mean when I delete a record in rails it will automatically delete from both joined tables? Mitch -- 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 -~----------~----~----~----~------~----~------~--~---
> > Thanks Fred. I have a foreign key in one of the tables. Does that > > mean > > when I delete a record in rails it will automatically delete from both > > joined tables? > > By default it will probably just raise an error. Check your database''s > documentationWould it depend on whether the belongs_to on that foreign key relation also has :dependent => :destroy? Does the cascading destroy recurse through multiple levels of association or does it only apply to the root object and it''s associations? On Jul 22, 4:21 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 22 Jul 2008, at 21:20, Mitchell Gould wrote: > > > > > > > Frederick Cheung wrote: > >> On 22 Jul 2008, at 18:43, Mitchell Gould wrote: > > >>> If I have two models that are in a one-to-one relationship in Ruby > >>> on > >>> Rails, > >>> and I wish to delete a record. > >>> How do I make sure I will delete all the related data from both > >>> tables? > > >>> Is there a simple elegant Ruby way to do this? > > >> rails provides the :dependant option. > >> If you want a cast iron guarantee then you need to get the database > >> to > >> do it for you (foreign key) > > >> Fred > > > Thanks Fred. I have a foreign key in one of the tables. Does that > > mean > > when I delete a record in rails it will automatically delete from both > > joined tables? > > By default it will probably just raise an error. Check your database''s > documentation > > Fred > > > > > Mitch > > -- > > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 22 Jul 2008, at 21:38, Robert Walker wrote:> >>> Thanks Fred. I have a foreign key in one of the tables. Does that >>> mean >>> when I delete a record in rails it will automatically delete from >>> both >>> joined tables? >> >> By default it will probably just raise an error. Check your >> database''s >> documentation > > Would it depend on whether the belongs_to on that foreign key relation > also has :dependent => :destroy?No - the rails world of dependant => :destroy and the database world are independent> Does the cascading destroy recurse > through multiple levels of association or does it only apply to the > root object and it''s associations?Might do - try it and find out :-) I would imagine so though. (but :dependent => :delete_all probably doesn''t recurse) Fred> > > On Jul 22, 4:21 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 22 Jul 2008, at 21:20, Mitchell Gould wrote: >> >> >> >> >> >>> Frederick Cheung wrote: >>>> On 22 Jul 2008, at 18:43, Mitchell Gould wrote: >> >>>>> If I have two models that are in a one-to-one relationship in Ruby >>>>> on >>>>> Rails, >>>>> and I wish to delete a record. >>>>> How do I make sure I will delete all the related data from both >>>>> tables? >> >>>>> Is there a simple elegant Ruby way to do this? >> >>>> rails provides the :dependant option. >>>> If you want a cast iron guarantee then you need to get the database >>>> to >>>> do it for you (foreign key) >> >>>> Fred >> >>> Thanks Fred. I have a foreign key in one of the tables. Does that >>> mean >>> when I delete a record in rails it will automatically delete from >>> both >>> joined tables? >> >> By default it will probably just raise an error. Check your >> database''s >> documentation >> >> Fred >> >> >> >>> Mitch >>> -- >>> 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 -~----------~----~----~----~------~----~------~--~---