From: "Esteban Manchado Velázquez" Date: 2005-04-24T03:31:45+09:00 Subject: Re: RoR has_and_belongs_to_many association Hi Jason, Thanks for your answer. Some more details below: On Sun, Apr 24, 2005 at 02:27:57AM +0900, Jason Foreman wrote: > [...] > 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. Sorry, I thought it wasn't necessary, and as the code is written in Spanish (for the RoR presentation), I didn't feel like translating :-) The main idea is having pupils and classes. You have a pupils table/model (alumnos) and a classes table/model (cursos). So I have: class Alumno class C < ActiveRecord::Base > belongs_to :a; > belongs_to :b; > # add your foo, bar attributes to this table. > end Hmmm... what about the "id" attribute? I know I can't simply put one, as it would clobber the alumnos or cursos one when using the h_a_b_t_m relation. And, how can i find a particular relation in this table? Should I just "find_all(:a_id => x, :b_id => y).first", or is there any way to obtain that object from the has_and_belongs_to_many association (I mean something like "alumno.cursos.first.relation" to get a AlumnosCursos class)? TIA. Regards, -- Esteban Manchado Vel�zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es