Let me preface this with the fact that I''ve been working with rails for about two weeks, so if I''m missing something obvious, feel free to give me a link and tell me to RTFM :) I''m working on a pretty simple photogallery application. I''ve just barely begun laying it out, but I will have several many to many relationships in the database. Ex. A single image may have multiple categories and each category may be attached to many images. Obviously I''ll have join tables to break it up into several one to many relationships. What my question is, while looking in the model functions like belongs_to, has_one, has_many etc, I see no way to easily describe this relationship. To access all categories for a given image, will I have to use a SQL or is there something in ActiveRecord that will ease this? Thanks, Tanner Burson
On Thu, Dec 16, 2004 at 03:44:19PM -0600, Tanner Burson wrote:> Let me preface this with the fact that I''ve been working with rails > for about two weeks, so if I''m missing something obvious, feel free to > give me a link and tell me to RTFM :) > > I''m working on a pretty simple photogallery application. I''ve just > barely begun laying it out, but I will have several many to many > relationships in the database. > > Ex. A single image may have multiple categories and each category may > be attached to many images. > > Obviously I''ll have join tables to break it up into several one to > many relationships. What my question is, while looking in the model > functions like belongs_to, has_one, has_many etc, I see no way to > easily describe this relationship. To access all categories for a > given image, will I have to use a SQL or is there something in > ActiveRecord that will ease this? > > Thanks, > > Tanner BursonWhat you want is the has_and_belongs_to_many relationship. So if Category has_and_belongs_to_many Images then you''d have a categories table, an images table, and a categories_images table. You could then do: @category = Category.find @params[''id''] @category.images.each do |image| # do something with the image end Etc. The API docs describe this relationship in good detail. -Scott
> What you want is the has_and_belongs_to_many relationship. So if > Category has_and_belongs_to_many Images then you''d have a categories > table, an images table, and a categories_images table. You could then > do: > > @category = Category.find @params[''id''] > @category.images.each do |image| > # do something with the image > endSorry for being thick... If I add the has_and_belongs_to_many to Images as well (or in place of in Category?) would I then be able to do the reverse? Is it typical to do this on both the (in the case of my example) Category and Image models? Or is it usually done on only one or the other?
Thijs van der Vossen
2004-Dec-16 22:18 UTC
Re: ActiveRecord and Many to Many relationships.
On Dec 16, 2004, at 23:08, Tanner Burson wrote:>> What you want is the has_and_belongs_to_many relationship. So if >> Category has_and_belongs_to_many Images then you''d have a categories >> table, an images table, and a categories_images table. You could then >> do: >> >> @category = Category.find @params[''id''] >> @category.images.each do |image| >> # do something with the image >> end > > Sorry for being thick... > > If I add the has_and_belongs_to_many to Images as well (or in place of > in Category?) would I then be able to do the reverse? Is it typical > to do this on both the (in the case of my example) Category and Image > models? Or is it usually done on only one or the other?Both. -- Fingertips - http://www.fngtps.com Firefox - Take back the web - http://getfirefox.com