# Avoiding HABTM field clobbering?

**URL:** <https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346>\
**Category:** Rails\
**Created:** [December 24, 2005, 9:54pm UTC](https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346 "2005-12-24T21:54:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [December 24, 2005, 9:54pm UTC](https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346/1 "2005-12-24T21:54:53Z")

</div>

> From the docs:

“Note that any fields in the join table will override  
matching field names in the two joined tables. As a  
consequence, having an “id” field in the join table  
usually has the undesirable result of clobbering the  
“id” fields in either of the other two tables.”

I do have an ID field in the join table, and as a  
result it’s clobbering the ID field of the associated  
table. It appears I can specify my own “finder\_sql”.  
Any other way? Perhaps a :select option would be nice?

thanks  
csn

* * *

Yahoo! for Good - Make a difference this year.  
[http://brand.yahoo.com/cybergivingweek2005/](http://brand.yahoo.com/cybergivingweek2005/)

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [December 25, 2005, 1:13am UTC](https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346/2 "2005-12-25T01:13:44Z")

</div>

I take it there must be some other data in this join table that you’re  
trying to keep track of–otherwise there would be no need for an id  
field.  
In that case, maybe you should think about turning the habtm relation  
into  
an intermediate Model, which has a belongs\_to relationship with each of  
the  
original models. Somebody talked about that just a week ago or so on  
this  
list. Something like this, I think:

class Reader  
has\_many :readings  
end

class Article  
has\_many :readings  
end

class Reading  
belongs\_to :reader  
belongs\_to :article  
end

hth  
Brian

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [December 25, 2005, 1:28am UTC](https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346/3 "2005-12-25T01:28:59Z")

</div>

The disadvantage to this method being that you can’t do @article.readers  
or @reader.articles. Of course this will be changing with the  
introduction of polymorphic associations in the next version of Rails:

class Reader  
has\_many :readings  
has\_many :articles, :through =\> :readings  
end

class Article  
has\_many :readings  
has\_many :readers, :through =\> :readings  
end

class Reading  
belongs\_to :reader  
belongs\_to :article  
end

Brian G. wrote:

> class Reader  
> has\_many :readings  
> end
> 
> class Article  
> has\_many :readings  
> end
> 
> class Reading  
> belongs\_to :reader  
> belongs\_to :article  
> end

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [December 25, 2005, 5:27pm UTC](https://www.ruby-forum.com/t/avoiding-habtm-field-clobbering/75346/4 "2005-12-25T17:27:41Z")

</div>

On 12/24/05, Marc L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The disadvantage to this method being that you can’t do @article.readers  
> or @reader.articles. Of course this will be changing with the  
> introduction of polymorphic associations in the next version of Rails:

You can do it, just manually. (i.e. create the Article#readers and  
Reader#articles methods by hand.) It’s what I’m doing now.
