Suggest using composite primary key on join tables in `:through` associations

This commit is contained in:
Nikita Vasilevsky 2023-09-08 21:38:24 +00:00
parent 0debdcf8dc
commit 5f69951cf2
No known key found for this signature in database
GPG Key ID: 0FF5725CD31059E4
1 changed files with 2 additions and 0 deletions

View File

@ -482,6 +482,8 @@ The simplest rule of thumb is that you should set up a `has_many :through` relat
You should use `has_many :through` if you need validations, callbacks, or extra attributes on the join model.
While `has_and_belongs_to_many` suggests creating join table with no primary key - `id: false`, consider using composite primary key for the join table in the `has_many :through` relationship. For example its recommended to use `create_table :manifests, primary_key: []:assembly_id, :part_id]` for the example above.
### Polymorphic Associations
A slightly more advanced twist on associations is the _polymorphic association_. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared: