Skip to content

Commit 16ff9af

Browse files
authored
Merge pull request rails#50275 from seanpdoyle/polymorphic-rename
Provide guidance for renaming classes in polymorphic associations [ci skip]
2 parents b1d57fb + 0d8b3f0 commit 16ff9af

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

actiontext/lib/action_text/attribute.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ module Attribute
3434
# * <tt>:strict_loading</tt> - Pass true to force strict loading. When
3535
# omitted, <tt>strict_loading:</tt> will be set to the value of the
3636
# <tt>strict_loading_by_default</tt> class attribute (false by default).
37+
#
38+
# Note: Action Text relies on polymorphic associations, which in turn store class names in the database.
39+
# When renaming classes that use <tt>has_rich_text</tt>, make sure to also update the class names in the
40+
# <tt>action_text_rich_texts.record_type</tt> polymorphic type column of
41+
# the corresponding rows.
3742
def has_rich_text(name, encrypted: false, strict_loading: strict_loading_by_default)
3843
class_eval <<-CODE, __FILE__, __LINE__ + 1
3944
def #{name}

activerecord/lib/active_record/associations.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,8 @@ def has_one(name, scope = nil, **options)
18251825
# Specify this association is a polymorphic association by passing +true+.
18261826
# Note: If you've enabled the counter cache, then you may want to add the counter cache attribute
18271827
# to the +attr_readonly+ list in the associated classes (e.g. <tt>class Post; attr_readonly :comments_count; end</tt>).
1828+
# Note: Since polymorphic associations rely on storing class names in the database, make sure to update the class names in the
1829+
# <tt>*_type</tt> polymorphic type column of the corresponding rows.
18281830
# [+:validate+]
18291831
# When set to +true+, validates new objects added to association when saving the parent object. +false+ by default.
18301832
# If you want to ensure associated objects are revalidated on every update, use +validates_associated+.

activestorage/lib/active_storage/attached/model.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ module Attached::Model
9797
# has_one_attached :avatar, strict_loading: true
9898
# end
9999
#
100+
# Note: Active Storage relies on polymorphic associations, which in turn store class names in the database.
101+
# When renaming classes that use <tt>has_one_attached</tt>, make sure to also update the class names in the
102+
# <tt>active_storage_attachments.record_type</tt> polymorphic type column of
103+
# the corresponding rows.
100104
def has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
101105
ActiveStorage::Blob.validate_service_configuration(service, self, name) unless service.is_a?(Proc)
102106

@@ -188,6 +192,10 @@ def #{name}=(attachable)
188192
# has_many_attached :photos, strict_loading: true
189193
# end
190194
#
195+
# Note: Active Storage relies on polymorphic associations, which in turn store class names in the database.
196+
# When renaming classes that use <tt>has_many</tt>, make sure to also update the class names in the
197+
# <tt>active_storage_attachments.record_type</tt> polymorphic type column of
198+
# the corresponding rows.
191199
def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false)
192200
ActiveStorage::Blob.validate_service_configuration(service, self, name) unless service.is_a?(Proc)
193201

guides/source/action_text_overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class MessagesController < ApplicationController
106106
end
107107
```
108108
109+
NOTE: Since Action Text relies on polymorphic associations, and [polymorphic associations](./association_basics.html#polymorphic-associations) rely on storing class names in the database, that data must remain synchronized with the class name used by the Ruby code. When renaming classes that use `has_rich_text`, make sure to also update the class names in the `action_text_rich_texts.record_type` polymorphic type column of the corresponding rows.
110+
109111
[`rich_text_area`]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-rich_text_area
110112
111113
## Rendering Rich Text Content

guides/source/active_storage_overview.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ end
475475

476476
Rails will enqueue a job to generate the variant after the attachment is attached to the record.
477477

478+
NOTE: Since Active Storage relies on polymorphic associations, and [polymorphic associations](./association_basics.html#polymorphic-associations) rely on storing class names in the database, that data must remain synchronized with the class name used by the Ruby code. When renaming classes that use `has_one_attached`, make sure to also update the class names in the `active_storage_attachments.record_type` polymorphic type column of the corresponding rows.
479+
478480
[`has_one_attached`]: https://api.rubyonrails.org/classes/ActiveStorage/Attached/Model.html#method-i-has_one_attached
479481
[Attached::One#attach]: https://api.rubyonrails.org/classes/ActiveStorage/Attached/One.html#method-i-attach
480482
[Attached::One#attached?]: https://api.rubyonrails.org/classes/ActiveStorage/Attached/One.html#method-i-attached-3F
@@ -549,6 +551,7 @@ end
549551
[Attached::Many#attach]: https://api.rubyonrails.org/classes/ActiveStorage/Attached/Many.html#method-i-attach
550552
[Attached::Many#attached?]: https://api.rubyonrails.org/classes/ActiveStorage/Attached/Many.html#method-i-attached-3F
551553

554+
NOTE: Since Active Storage relies on polymorphic associations, and [polymorphic associations](./association_basics.html#polymorphic-associations) rely on storing class names in the database, that data must remain synchronized with the class name used by the Ruby code. When renaming classes that use `has_many_attached`, make sure to also update the class names in the `active_storage_attachments.record_type` polymorphic type column of the corresponding rows.
552555

553556
### Attaching File/IO Objects
554557

guides/source/association_basics.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ class CreatePictures < ActiveRecord::Migration[7.2]
538538
end
539539
```
540540

541+
NOTE: Since polymorphic associations rely on storing class names in the
542+
database, that data must remain synchronized with the class name used by the
543+
Ruby code. When renaming a class, make sure to update the data in the
544+
polymorphic type column.
545+
541546
![Polymorphic Association Diagram](images/association_basics/polymorphic.png)
542547

543548
### Associations between Models with Composite Primary Keys

0 commit comments

Comments
 (0)