Skip to content

Commit

Permalink
Fix attaching blobs via nested attributes
Browse files Browse the repository at this point in the history
Closes #37411.
  • Loading branch information
choznerol committed Aug 4, 2020
1 parent b39ea7c commit eb5f5ed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Expand Up @@ -65,7 +65,7 @@ def find_or_build_blob
**attachable.reverse_merge(
record: record,
service_name: attachment_service_name
)
).symbolize_keys
)
when String
ActiveStorage::Blob.find_signed!(attachable, record: record)
Expand Down
7 changes: 7 additions & 0 deletions activestorage/test/models/attached/one_test.rb
Expand Up @@ -319,6 +319,13 @@ def upload.open
assert_equal 2736, @user.avatar.metadata[:height]
end

test "creating an attachment as part of an autosave association through nested attributes" do
group = Group.create!(users_attributes: [{ name: "John", avatar: { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" } }])
group.save!
new_user = User.find_by(name: "John")
assert new_user.avatar.attached?
end

test "updating an attachment as part of an autosave association" do
group = Group.create!(users: [@user])
@user.avatar = fixture_file_upload("racecar.jpg")
Expand Down
2 changes: 2 additions & 0 deletions activestorage/test/test_helper.rb
Expand Up @@ -127,6 +127,8 @@ class User < ActiveRecord::Base
class Group < ActiveRecord::Base
has_one_attached :avatar
has_many :users, autosave: true

accepts_nested_attributes_for :users
end

require_relative "../../tools/test_common"

0 comments on commit eb5f5ed

Please sign in to comment.