Is there a way to update attributes in a HABTM join table? Right now I am deleting the join and then recreating it with new attributes. I have a table "categories" and another table "items". They are both HABTM with the join table "categories_items" between them. If an item is joined to a category once, no problem, we create the join. But if an item is joined to a category more than once, I want to record the subsequent hit in a field called "cat_count" which exists in the join table. def add_category( category_id_as_str ) category = Category.find(category_id_as_str.to_i) if self.categories.include?(category) old_count = self.categories.find(category.id).cat_count.to_i self.categories.delete(category) else old_count = 0 end self.categories.push_with_attributes(category, :cat_count => old_count + 1) end It seems like there should be a way to update the join instead of deleting and recreating it. I know I could always turn the join into a model--I''ve done that before but it seems like overkill for something so simple as this. If it knows both unique ids, it should possible. Is there a way to update a HABTM join attribute with a method similar to push_with_attributes? Seems like there should be something like: Object.collection.update_attributes(collection_id, attribute => value) Kevin
Mark Reginald James
2006-Jan-05 00:01 UTC
[Rails] Re: Updating Attributes in a HABTM Join Table
Kevin Skoglund wrote:> Is there a way to update attributes in a HABTM join table? Right now I > am deleting the join and then recreating it with new attributes.http://article.gmane.org/gmane.comp.lang.ruby.rails/38103 -- We develop, watch us RoR, in numbers too big to ignore.
Kevin Skoglund
2006-Jan-05 15:21 UTC
[Rails] Re: Updating Attributes in a HABTM Join Table
> From: Mark Reginald James <mrj@bigpond.net.au> > Date: January 4, 2006 7:00:25 PM EST>> Is there a way to update attributes in a HABTM join table? Right >> now I am deleting the join and then recreating it with new >> attributes. > > http://article.gmane.org/gmane.comp.lang.ruby.rails/38103Thanks, Mark. That link was helpful. I had considered just writing the same SQL myself. So I guess the answer to my question is that there is a way, but it''s not part of ActiveRecord. IMHO, this should be part of the next release. After all, the code is already written... Kevin