Skip to content

Commit c1d7d56

Browse files
authored
Merge pull request rails#55107 from rails/fxn/through-docs
Document through + polymorphic
2 parents 6e4a09e + 77eb079 commit c1d7d56

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,10 @@ module ClassMethods
12081208
# [+:as+]
12091209
# Specifies a polymorphic interface (See #belongs_to).
12101210
# [+:through+]
1211-
# Specifies an association through which to perform the query. This can be any other type
1212-
# of association, including other <tt>:through</tt> associations. Options for <tt>:class_name</tt>,
1211+
# Specifies an association through which to perform the query.
1212+
#
1213+
# This can be any other type of association, including other <tt>:through</tt> associations,
1214+
# but it cannot be a polymorphic association. Options for <tt>:class_name</tt>,
12131215
# <tt>:primary_key</tt> and <tt>:foreign_key</tt> are ignored, as the association uses the
12141216
# source reflection.
12151217
#
@@ -1411,10 +1413,12 @@ def has_many(name, scope = nil, **options, &extension)
14111413
# [+:as+]
14121414
# Specifies a polymorphic interface (See #belongs_to).
14131415
# [+:through+]
1414-
# Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt>,
1415-
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
1416-
# source reflection. You can only use a <tt>:through</tt> query through a #has_one
1417-
# or #belongs_to association on the join model.
1416+
# Specifies an association through which to perform the query.
1417+
#
1418+
# This can be any other type of association, including other <tt>:through</tt> associations,
1419+
# but it cannot be a polymorphic association. Options for <tt>:class_name</tt>, <tt>:primary_key</tt>,
1420+
# and <tt>:foreign_key</tt> are ignored, as the association uses the source reflection. You can only
1421+
# use a <tt>:through</tt> query through a #has_one or #belongs_to association on the join model.
14181422
#
14191423
# If the association on the join model is a #belongs_to, the collection can be modified
14201424
# and the records on the <tt>:through</tt> model will be automatically created and removed

guides/source/association_basics.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ object, use the `collection.build` method.
953953
A [`has_many :through`][`has_many`] association is often used to set up a
954954
many-to-many relationship with another model. This association indicates that
955955
the declaring model can be matched with zero or more instances of another model
956-
by proceeding _through_ a third model.
956+
by proceeding _through_ an intermediate "join" model.
957957

958958
For example, consider a medical practice where patients make appointments to see
959959
physicians. The relevant association declarations could look like this:
@@ -1012,6 +1012,9 @@ In this migration the `physicians` and `patients` tables are created with a
10121012
created with `physician_id` and `patient_id` columns, establishing the
10131013
many-to-many relationship between `physicians` and `patients`.
10141014

1015+
INFO: The through association can be any type of association, including other
1016+
through associations, but it cannot be a polymorphic association.
1017+
10151018
You could also consider using a [composite primary
10161019
key](active_record_composite_primary_keys.html) for the join table in the
10171020
`has_many :through` relationship like below:
@@ -1150,6 +1153,9 @@ class CreateAccountHistories < ActiveRecord::Migration[8.1]
11501153
end
11511154
```
11521155

1156+
INFO: The through association can be any type of association, including other
1157+
through associations, but it cannot be a polymorphic association.
1158+
11531159
### `has_and_belongs_to_many`
11541160

11551161
A [`has_and_belongs_to_many`][] association creates a direct many-to-many

0 commit comments

Comments
 (0)