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
easy way to update a HABTM join’s attributes. I usually have to delete
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
thing, I think they get clobbered.
Or you could create your own model for the join table. However this
own problems. Do you make a fake autonumber/serial primary key field for
table then? If you do, Rails should work fine, although you’ve lost the
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
Update and destroy methods will not work (I usually override the update
in the model with a custom SQL statement). Record inserts will work,
Rails automatically tries to go get the ID field from the last inserted
PostgreSQL, by selecting the current value from an appropriately named
sequence). But since the PK is not an autonumber/serial, there is no
so Rails fails. I get around this by setting the (nonexistant) ID
before I save it (model_record[‘id’] = some_bogus_value). That seems to
Rails well enough.
Really, the HABTM support in Rails is pretty lame. It’s been one of the
frustrating aspects of ActiveRecord for me. Unfortunately, given how
is about every table having an ID primary key, I don’t see this getting