Forum: Ruby on Rails How to retrieve attributes from HABTM?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
548240c1431285c235ecf711293a7311?d=identicon&s=25 Yash (Guest)
on 2006-03-01 16:23
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
720fde074c77f4c9692dd6f05802d581?d=identicon&s=25 Nithin Reddy (jashugan)
on 2006-03-01 20:45
(Received via mailing list)
Why don't you create a model for the join table?
27f8beb57f4d56d328749b51ac886d2c?d=identicon&s=25 KW (Guest)
on 2006-03-02 00:47
(Received via mailing list)
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
>  <at> 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.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (Guest)
on 2006-03-02 01:06
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

--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
>>  <at> 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?
65df3477a529ccf32e1b321c14b47668?d=identicon&s=25 Jay Wiggins (Guest)
on 2006-03-02 12:40
(Received via mailing list)
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
F59329dc91cba06600ff65c85fd3e93c?d=identicon&s=25 Anthony Green (acgreen)
on 2006-03-02 14:30
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
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-04-16 07:41
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....
This topic is locked and can not be replied to.