# How to retrieve attributes from HABTM?

**URL:** <https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250>\
**Category:** Rails\
**Created:** [March 1, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250 "2006-03-01T16:23:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 1, 2006, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/1 "2006-03-01T16:23:08Z")

</div>

We have tables Users and Communities linked by has\_an\_belongs\_to\_many.  
The join table Communities\_Users has additional fields [ is\_active,  
is\_blocked, join\_date] etc. These are populated using  
@user.push\_with\_attributes(:is\_active =\> true,…).  
Later on how do we update or retrieve the attributes in the link table  
for a given user or a community with doing it explicitly using SQL?

Thanks,  
Yash

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 1, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/2 "2006-03-01T20:45:09Z")

</div>

Why don’t you create a model for the join table?

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 2, 2006, 12:47am UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/3 "2006-03-02T00:47:31Z")

</div>

Yash \<yashgt@…\> writes:

> We have tables Users and Communities linked by has\_an\_belongs\_to\_many.  
> The join table Communities\_Users has additional fields [ is\_active,  
> is\_blocked, join\_date] etc. These are populated using  
> user.push\_with\_attributes(:is\_active =\> true,…).  
> Later on how do we update or retrieve the attributes in the link table  
> for a given user or a community with doing it explicitly using SQL?

Without creating a separate model for the join table, there appears to  
be no  
easy way to update a HABTM join’s attributes. I usually have to delete  
the  
associated object and add it back in again with the changed atttributes.

Retrieval is OK. community.users should provide you with the join table  
attributes merged into the users attributes. If have any fields named  
the same  
thing, I think they get clobbered.

Or you could create your own model for the join table. However this  
involves its  
own problems. Do you make a fake autonumber/serial primary key field for  
the  
table then? If you do, Rails should work fine, although you’ve lost the  
purity  
of the join table in the process.

If you choose leave your table with a composite primary key of (user\_id,  
community\_id) (or whatever), Rails will have a very hard time dealing  
with it.  
Update and destroy methods will not work (I usually override the update  
method  
in the model with a custom SQL statement). Record inserts will work,  
although  
Rails automatically tries to go get the ID field from the last inserted  
row (in  
PostgreSQL, by selecting the current value from an appropriately named  
sequence). But since the PK is not an autonumber/serial, there is no  
sequence,  
so Rails fails. I get around this by setting the (nonexistant) ID  
attribute  
before I save it (model\_record[‘id’] = some\_bogus\_value). That seems to  
fool  
Rails well enough.

Really, the HABTM support in Rails is pretty lame. It’s been one of the  
most  
frustrating aspects of ActiveRecord for me. Unfortunately, given how  
zealous DHH  
is about every table having an ID primary key, I don’t see this getting  
fixed  
anytime soon.

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 2, 2006, 1:06am UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/4 "2006-03-02T01:06:24Z")

</div>

I’ll chime in on going with the join model approach. It just works  
better with Rails. I’m doing a multi-part essay on my blog about things  
you can do with them, so instead of repeating myself I’ll refer you  
there 🙂

[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

–josh

KW wrote:

> Yash \<yashgt@…\> writes:
> 
> > We have tables Users and Communities linked by has\_an\_belongs\_to\_many.  
> > The join table Communities\_Users has additional fields [ is\_active,  
> > is\_blocked, join\_date] etc. These are populated using  
> > user.push\_with\_attributes(:is\_active =\> true,…).  
> > Later on how do we update or retrieve the attributes in the link table  
> > for a given user or a community with doing it explicitly using SQL?

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 2, 2006, 2:30pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/5 "2006-03-02T14:30:29Z")

</div>

Yash wrote:

> We have tables Users and Communities linked by has\_an\_belongs\_to\_many.  
> The join table Communities\_Users has additional fields [ is\_active,  
> is\_blocked, join\_date] etc. These are populated using  
> @user.push\_with\_attributes(:is\_active =\> true,…).  
> Later on how do we update or retrieve the attributes in the link table  
> for a given user or a community with doing it explicitly using SQL?

As the others have said this is a good case for a separate domain model  
say Membership. See the Agile book page 241

\_tony

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [April 16, 2007, 7:41am UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/6 "2007-04-16T07:41:09Z")

</div>

you could also use sql to update the join table:

ActiveRecord::Base.connection.execute(sql)

should work fine. However, if you really see yourself using and  
interacting with the join table, go the :through route…

---

<div class="post-metadata">

**Author:** ![yash](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/f17d59/32.png) [@yash](https://www.ruby-forum.com/u/yash)\
**Post date:** [March 2, 2006, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-retrieve-attributes-from-habtm/75250/7 "2006-03-02T12:40:57Z")

</div>

is this the same issue? although i’m too newby to know if it’s just  
me, i’ve run into problems with HABTM as well

[http://dev.rubyonrails.org/ticket/1031](http://dev.rubyonrails.org/ticket/1031)
