# Updating attributes in HABTM association

**URL:** <https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878>\
**Category:** Rails\
**Created:** [April 5, 2006, 11:04am UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878 "2006-04-05T11:04:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 11:04am UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/1 "2006-04-05T11:04:13Z")

</div>

Hi,

I am trying to find a way tp update attributes in a habtm association. I  
am trying to use the code from ticket #2462, but as my  
association\_class\_primary\_key\_name seems to be empty the generated sql  
code is corrupt. I hope thats not a problem of my model that this  
variable is empty.  
What would you recommend to update those attributes (rails 1.1)

THANKS!!!

---

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/2 "2006-04-05T18:43:49Z")

</div>

Henrik Badekow wrote:

> Hi,
> 
> I am trying to find a way tp update attributes in a habtm association. I  
> am trying to use the code from ticket #2462, but as my  
> association\_class\_primary\_key\_name seems to be empty the generated sql  
> code is corrupt. I hope thats not a problem of my model that this  
> variable is empty.  
> What would you recommend to update those attributes (rails 1.1)
> 
> THANKS!!!

Could you post the code you’re using?

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/3 "2006-04-05T18:51:04Z")

</div>

Henrik Badekow wrote:

> I am trying to find a way tp update attributes in a habtm association. I  
> am trying to use the code from ticket #2462, but as my  
> association\_class\_primary\_key\_name seems to be empty the generated sql  
> code is corrupt. I hope thats not a problem of my model that this  
> variable is empty.  
> What would you recommend to update those attributes (rails 1.1)

There is no recommended way to update attributes on a habtm association.  
In fact, rich associations (habtm with attributes) have been deprecated  
and won’t be supported at all come 2.0 release. Use a join model  
(has\_many :through) instead.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/4 "2006-04-05T20:48:35Z")

</div>

Josh S. wrote:

> There is no recommended way to update attributes on a habtm association.  
> In fact, rich associations (habtm with attributes) have been deprecated  
> and won’t be supported at all come 2.0 release. Use a join model  
> (has\_many :through) instead.

Correct me if I’m wrong, but these are technically called “polymorphic  
joins”  
aren’t they? I would recommend we try to use the right terms when we  
can, so  
that hopefully everyone is on the same page. Saying just “use a join  
model”  
easily means more than one thing, just in a Rails context, doesn’t it?

-Brian

---

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/5 "2006-04-05T22:13:45Z")

</div>

Brian V. Hughes wrote:

> Josh S. wrote:
> 
> > There is no recommended way to update attributes on a habtm association.  
> > In fact, rich associations (habtm with attributes) have been deprecated  
> > and won’t be supported at all come 2.0 release. Use a join model  
> > (has\_many :through) instead.
> 
> Correct me if I’m wrong, but these are technically called “polymorphic  
> joins”  
> aren’t they? I would recommend we try to use the right terms when we  
> can, so  
> that hopefully everyone is on the same page. Saying just “use a join  
> model”  
> easily means more than one thing, just in a Rails context, doesn’t it?

Brian, I don’t think Henrik was talking about polymorphism. The usual  
habtm join table has nothing to do with polymorphism, and I’m not sure  
how to make it work with polymorphic associations anyway.

I’m pretty sure I was using the right terminology. In Rails parlance a  
“join model” is a join table that has been promoted to full model status  
by adding a primary key and probably other attribute fields as well. It  
also has a Model class, which habtm join tables don’t, so its table name  
corresponds to its model name in the usual fashion, rather than being  
constructed from names of the tables it is joining.

You can use polymorphic associations in a join model, but it gets  
tricky. I just blogged about that a couple days ago in fact.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![hennebausv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f14d63/32.png) [@hennebausv](https://www.ruby-forum.com/u/hennebausv)\
**Post date:** [April 5, 2006, 10:32pm UTC](https://www.ruby-forum.com/t/updating-attributes-in-habtm-association/56878/6 "2006-04-05T22:32:49Z")

</div>

Josh S. wrote:

> Brian, I don’t think Henrik was talking about polymorphism. The usual  
> habtm join table has nothing to do with polymorphism, and I’m not sure  
> how to make it work with polymorphic associations anyway.

Hmm… OK. I thought that the has\_many :through designation is what  
signified a  
polymorphic join. Obviously, I have that wrong. 🙂

> I’m pretty sure I was using the right terminology. In Rails parlance a  
> “join model” is a join table that has been promoted to full model status  
> by adding a primary key and probably other attribute fields as well. It  
> also has a Model class, which habtm join tables don’t, so its table name  
> corresponds to its model name in the usual fashion, rather than being  
> constructed from names of the tables it is joining.

Sure. I’m willing to go with that. Like I said, the real goal of my  
email was to  
make sure we’re all using the same (or mostly the same) vocab,  
especially when  
talking about some of the more involved Rails concepts/features.

> You can use polymorphic associations in a join model, but it gets  
> tricky. I just blogged about that a couple days ago in fact.

Well, I’ll definitely need to check out that post so I can figure out  
what the  
hell I thought I was talking about! ;-\>

-Brian
