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

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

+4
source share
2 answers

Yes, you need a connection table. This shows how: http://railscasts.com/episodes/47-two-many-to-many

It may also be helpful; has_many: through questions

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

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

All of these are the views you might want to make :)

Index is optional and also depends on db. mySQL is only used to support 1 at a time. Not sure if this has changed.

+7
source

It depends on how you want to use the has_many :through relation. There are two different cases (named in the relationship guide

  • for the n: m relationship: an example in the manual - doctors have meetings with patients.
  • for 1: n and an additional ratio of 1: n: an example in the manual: 1 document has many sections, and 1 section has many paragraphs.

For the first you need a connection table, you do not need an index. For the second, you do not need any of them.

+1
source

All Articles