What is Best Way of Tackling Rising Data Anomalies

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)

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