Maintaining referental Integrity

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

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

Frederick Cheung wrote:

Frederick Cheung 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

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

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 :slight_smile: I would imagine so though.
(but :dependent => :delete_all probably doesn't recurse)

Fred