I haven''t quite managed to wrap my brain around how associations work in Rails. I hope the book will help me with that when it arrives. I have the controllers "mob" and "drop". A mob has many drops, a drop belongs to many mobs. Mobs is clearly has_many :drops. Drops feel like it should be has_and_belongs_to_many :mobs. I have the table drops_mobs to store the association between mob/drop IDs. What I then want is a list of drops for a mob, but this is where I fail. Could someone give me a pointer? -- Johan Svensson http://atomicplayboy.net/
They''d both be has_and_belongs_to_many...I think. I think if one is habtm, they both are. In this case, all you have to do to get a list of drops for a particular mob is mob.drops On 8/2/05, Johan Svensson <echo5ive-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I haven''t quite managed to wrap my brain around how associations work > in Rails. I hope the book will help me with that when it arrives. > > I have the controllers "mob" and "drop". > > A mob has many drops, a drop belongs to many mobs. > > Mobs is clearly has_many :drops. Drops feel like it should be > has_and_belongs_to_many :mobs. I have the table drops_mobs to store > the association between mob/drop IDs. > > What I then want is a list of drops for a mob, but this is where I > fail. Could someone give me a pointer? > > -- > Johan Svensson > http://atomicplayboy.net/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hi Johan, If you have a join table, then you will use has_and_belongs_to_many for both of them. So, you model for mob will be has_and_belongs_to_many :drops and for drop: has_and_belongs_to_many :mobs Then, from a @mob, you could say @mob.drops for the drops, and from a @drop, @drop.mobs. If you haven''t read this page, it should hopefully help: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html I believe only the has_and_belongs_to_many uses a join table. For the others, the easy rule is, the belongs_to association goes with the table that has the foreign *_id column. The other table is either a has_one, or has_many association. Good Luck. Tom On 8/2/05, Johan Svensson <echo5ive-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I haven''t quite managed to wrap my brain around how associations work > in Rails. I hope the book will help me with that when it arrives. > > I have the controllers "mob" and "drop". > > A mob has many drops, a drop belongs to many mobs. > > Mobs is clearly has_many :drops. Drops feel like it should be > has_and_belongs_to_many :mobs. I have the table drops_mobs to store > the association between mob/drop IDs. > > What I then want is a list of drops for a mob, but this is where I > fail. Could someone give me a pointer? > > -- > Johan Svensson > http://atomicplayboy.net/ > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >