From 5f69951cf2cf23bbc4541d8c496c613b3a8310a9 Mon Sep 17 00:00:00 2001 From: Nikita Vasilevsky Date: Fri, 8 Sep 2023 21:38:24 +0000 Subject: [PATCH] Suggest using composite primary key on join tables in `:through` associations --- guides/source/association_basics.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md index ba58152d1a7..facdd64e638 100644 --- a/guides/source/association_basics.md +++ b/guides/source/association_basics.md @@ -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: