# What is Best Way of Tackling Rising Data Anomalies

**URL:** <https://www.ruby-forum.com/t/what-is-best-way-of-tackling-rising-data-anomalies/141253>\
**Category:** Rails\
**Created:** [June 24, 2008, 2:49am UTC](https://www.ruby-forum.com/t/what-is-best-way-of-tackling-rising-data-anomalies/141253 "2008-06-24T02:49:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 2:49am UTC](https://www.ruby-forum.com/t/what-is-best-way-of-tackling-rising-data-anomalies/141253/1 "2008-06-24T02:49:06Z")

</div>

Here is a problem: while the user is adding , deleting and modifying  
data , data anomalies can arise, for example take the the two table A, B  
which are joined by the intermediate join table A\_B:

[A]  
id:integer  
name:string

[A\_B]  
a\_id:integer  
b\_id:integer

[B]  
id:integer  
name:string

Now I have in A’s model file:  
has\_many :a\_b, :dependent =\> :destroy

and in B’s model file :  
has\_many :a\_b, :dependent =\> :destroy

Apparently sometimes the dependent destroy directive is not taking  
place.

For example B is deleted from the database but the deleted record’s id  
remains in A\_B. This causes nil.nilclass exceptions ; what is the best  
way of handling this? [more explanation see 1]

How to gracefully tackle these data anomalies that may arise?

{1}Elaboration :

example  
[A]  
[1:TestA]

[A\_B]  
[1,1]

[B]  
[1:TestB]

–  
Now despite the model file directive sometimes [B] is empty but [A\_B]  
remains and when I try to access A.A\_B.name I get a nil class exception.  
Currently when I encounter this errant intermediate record I delete and  
move on but any other features if they exist would be great to learn  
(sorry if my expectations are through the roof, but ROR is great)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 24, 2008, 3:22am UTC](https://www.ruby-forum.com/t/what-is-best-way-of-tackling-rising-data-anomalies/141253/2 "2008-06-24T03:22:21Z")

</div>

> –  
> Now despite the model file directive sometimes [B] is empty but [A\_B]  
> remains and when I try to access A.A\_B.name I get a nil class exception.  
> Currently when I encounter this errant intermediate record I delete and  
> move on but any other features if they exist would be great to learn  
> (sorry if my expectations are through the roof, but ROR is great)

As far as I’m concerned, if you want a cast iron guarantee that your  
foo\_id columns are just dangling then that guarentee has to be  
provided by the database (ie a foreign key constraint)

Fred
