From: Jason Foreman Date: 2005-04-24T02:27:57+09:00 Subject: Re: RoR has_and_belongs_to_many association On 4/22/05, Esteban Manchado Vel�zquez wrote: > Hi all, > > I'm writing a small RoR application to make a presentation (I'll publish > the LaTeX/PDF slides and the app, but in Spanish), and I'm using the > has_and_belongs_to_many association. > > I have this: > > - Table A and table B, associated with h_a_b_t_m > - Table A_b with _additional fields_ (a_id, b_id, foo and bar) > > The question is, how can I update the "foo" and "bar" values? When I call > a.bs, I can read the values of those fields, but updating doesn't seem to > work. It doesn't raise any exception, but doesn't update the database either. > What am I doing wrong? Is there any easy way to update the "foo" and "bar" > fields? > Many times a many-many relationship like this is really another piece of your model that you need to work with, particularly when you have additional information associated with the relationship. You might need to add a model C which represents the relationship of a<->b and contains those additional attributes. You haven't given any details as to what your A and B are, so I can't give you any insight into what the relationship model might be, but consider modelling it as its own class. class C < ActiveRecord::Base belongs_to :a; belongs_to :b; # add your foo, bar attributes to this table. end Jason