aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2018-01-08 14:34:03 -0500
committerGeorge Claghorn <george@basecamp.com>2018-01-08 14:34:03 -0500
commit93e6a0ec55a93fbb20d301763b69029b71d49fe9 (patch)
tree226f4cff80434093ca471ac5771fb8d63366912b /activestorage
parentedcd32edadd0d6d6b505f59436d05c6a890d4d7c (diff)
downloadrails-93e6a0ec55a93fbb20d301763b69029b71d49fe9.tar.gz
rails-93e6a0ec55a93fbb20d301763b69029b71d49fe9.tar.bz2
rails-93e6a0ec55a93fbb20d301763b69029b71d49fe9.zip
Fix attaching blobs to optimistically-locked records
Explicitly declare inverse associations so ActiveStorage::Attachment touches the original record instance. Closes #31542.
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/lib/active_storage/attached/macros.rb4
1 files changed, 2 insertions, 2 deletions
diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb
index 2b38a9b887..442dcabd0c 100644
--- a/activestorage/lib/active_storage/attached/macros.rb
+++ b/activestorage/lib/active_storage/attached/macros.rb
@@ -38,7 +38,7 @@ module ActiveStorage
end
CODE
- has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
+ has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record
has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob
scope :"with_attached_#{name}", -> { includes("#{name}_attachment": :blob) }
@@ -83,7 +83,7 @@ module ActiveStorage
end
CODE
- has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"
+ has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob
scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) }