Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a join table for a has_many :through association?

I've only created a has_and_belongs_to_many association before and it is different from has_many :through. For a has_many :through association, do I need a join table? How does the actual association work? Do I need an index? I can't find a great tutorial on this, any suggestions?

like image 573
Matthew Berman Avatar asked Feb 22 '23 17:02

Matthew Berman


1 Answers

Yes you need the join table. This shows how: http://railscasts.com/episodes/47-two-many-to-many

This may also be helpful; has_many :through questions

By the way if you need to search with condition this will help: Has many through associations with conditions

Also a great example with code of editing the nested attributes of the join table at Rails nested form with has_many :through, how to edit attributes of join model?.

All these are sorts of things you might find yourself wanting to do :)

Index are optional and also vary by db. mySQL used to only support 1 at a time. Not sure if that has changed.

like image 195
Michael Durrant Avatar answered Apr 08 '23 21:04

Michael Durrant