# Maintaining referental Integrity

**URL:** <https://www.ruby-forum.com/t/maintaining-referental-integrity/143787>\
**Category:** Rails\
**Created:** [July 22, 2008, 7:43pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787 "2008-07-22T19:43:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 7:43pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/1 "2008-07-22T19:43:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/2 "2008-07-22T21:24:20Z")

</div>

On 22 Jul 2008, at 18:43, Mitchell G. 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

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/3 "2008-07-22T22:20:09Z")

</div>

Frederick C. wrote:

> On 22 Jul 2008, at 18:43, Mitchell G. 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?  
Mitch

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/4 "2008-07-22T22:21:52Z")

</div>

On 22 Jul 2008, at 21:20, Mitchell G. wrote:

> > > tables?  
> > > 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

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/5 "2008-07-22T22:38:52Z")

</div>

> > 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? 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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 22, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/maintaining-referental-integrity/143787/6 "2008-07-22T23:01:46Z")

</div>

On 22 Jul 2008, at 21:38, Robert W. wrote:

> 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
