# Association data clobbering (foreign keys too?)

**URL:** <https://www.ruby-forum.com/t/association-data-clobbering-foreign-keys-too/60784>\
**Category:** Rails\
**Created:** [May 17, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/association-data-clobbering-foreign-keys-too/60784 "2006-05-17T14:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 17, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/association-data-clobbering-foreign-keys-too/60784/1 "2006-05-17T14:18:56Z")

</div>

Can someone please confirm or correct the following statements?

If I have the following tables

create table as (id int, […], b\_id int);  
create table bs (id int, […], a\_id int);  
create table as\_bs (a\_id int, b\_id int);

and the associations woould be defined like this

class A \<\< …  
habtm :bs  
belongs\_to :b  
end

so my Model A has a habtm collection of Bs _plus_ a direct belongs\_to  
link to one B (and vice versa).

So

a = A.find(:first)  
b = a.bs.first  
a2 = b.a

might not give me back a, because b’s direct belongs\_to a reference is  
clobbered with the reference from the join table a\_b (because the  
foreign keys have the same name)?

Or does this only apply to non foreign key attributes?

There are no such problems if I’d make a\_b a join model and have no  
habtm, but a has\_many :through instead?

What other options to circumvent this problem do I have (besides  
renaming the foreign keys either in the model or join tables)?

TIA! - Bernd

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 18, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/association-data-clobbering-foreign-keys-too/60784/2 "2006-05-18T12:58:47Z")

</div>

Not sure I can help but I think you’re trying to create a HABTM between  
model A and model B. That’s fairly straightforward.

It’s the other relationship I’m struggling to understand - is that a 1:1  
1:many or many:many?

Let’s say it’s a 1:many, with model A has many Bs. You’d then want  
has\_many in the A model declaration, and belongs\_to in the B  
declaration. Also, you’d only want the foreign key column (a\_id) in  
table bs (and not belongs\_to in A, also no foreign key column in as)

For sake of clarity, you might want to call this relationship something  
else (has\_many :alt\_bs, and then explicitly state the table name and  
foreign key in the has\_many declaration, as per the API).

Does this help? If you can supply some more details that’d be good.

HTH

---

<div class="post-metadata">

**Author:** ![msojka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@msojka](https://www.ruby-forum.com/u/msojka)\
**Post date:** [May 19, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/association-data-clobbering-foreign-keys-too/60784/3 "2006-05-19T12:05:46Z")

</div>

Chris T wrote:

> For sake of clarity, you might want to call this relationship something  
> else (has\_many :alt\_bs, and then explicitly state the table name and  
> foreign key in the has\_many declaration, as per the API).

Although I have some (little) experience with rails associations it  
seems like I misunderstood the clobbering effects, because I expected  
them to be “generated” on DB, not rails level.

So on other words, there are no ambiguity problems that are causing data  
clobbering when I’m using the same foreign key names in the model and  
join tables? (Although I already had this problem when one of the  
foreign keys in a “legacy” join table has been named “id”, but that  
might have been another problem).

Just to answer your question about the other relationship: This could  
either be a 1:many or 1:1. In my example I used a completely symmetric  
model. I’ll give an example with “real” objects:

table clients (  
id int,  
…,  
address\_id int – think of this as primary address  
);

table addresses (  
id int,  
…,  
client\_id int – think of this as primary client (although this might  
not make much sense)  
);

table addresses\_clients (  
address\_id int, – same foreign key name as in clients  
client\_id int – same foreign key name as in addresses  
);

class Client \< …  
has\_and\_belongs\_to\_many :addresses  
belongs\_to :address # or should it better be  
primary\_address?  
end

class Address \< …  
has\_and\_belongs\_to\_many :clients  
belongs\_to :client # or some other name  
end

Now:

c = Client.find(1)  
a = c.addresses.first # get one from the collection  
c2 = a.client

Assuming that a.client is pointing to c, may I have clobbering effects  
(a.client being “shadowed” by the client\_id from the join table which  
might point to some other client)?
