Skip to content

Instantly share code, notes, and snippets.

@robvitaro
Last active September 3, 2020 13:43
Show Gist options
  • Save robvitaro/1b9ad26b685d7881d17ed9acd48ba7dd to your computer and use it in GitHub Desktop.
Save robvitaro/1b9ad26b685d7881d17ed9acd48ba7dd to your computer and use it in GitHub Desktop.
A bug I discovered with duplicating ActiveStorage records and the .attached? method. Rails issue: https://github.com/rails/rails/issues/40166
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "rails", "6.0.3"
gem "sqlite3"
end
require "active_record/railtie"
require "active_storage/engine"
require "tmpdir"
class TestApp < Rails::Application
config.root = __dir__
config.hosts << "example.org"
config.eager_load = false
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.active_storage.service = :local
config.active_storage.service_configurations = {
local: {
root: Dir.tmpdir,
service: "Disk"
}
}
end
ENV["DATABASE_URL"] = "sqlite3::memory:"
Rails.application.initialize!
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s
ActiveRecord::Schema.define do
CreateActiveStorageTables.new.change
create_table :users, force: true
end
class User < ActiveRecord::Base
has_one_attached :profile
end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_duplicate_without_checking_attached
# create original user
user = User.create!(
profile: {
content_type: "text/plain",
filename: "dummy.txt",
io: ::StringIO.new("dummy"),
}
)
# duplicate user
new_user = user.dup
new_user.save!
# duplicate attachment
image_io = user.profile.download
ct = user.profile.content_type
fn = user.profile.filename
new_blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new(image_io),
filename: fn,
content_type: ct,
)
# attach duplicated profile to new_user
new_user.profile.attach(new_blob)
# most recent attachment's record_id will match new_user.id
assert_equal new_user.id, ActiveStorage::Attachment.last.record_id
end
def test_duplicate_checking_attached_first
# create original user
user = User.create!(
profile: {
content_type: "text/plain",
filename: "dummy.txt",
io: ::StringIO.new("dummy"),
}
)
# check for attachments on user first <-- source of bug?
user.profile.attached?
# duplicate user
new_user = user.dup
new_user.save!
# duplicate attachment
image_io = user.profile.download
ct = user.profile.content_type
fn = user.profile.filename
new_blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new(image_io),
filename: fn,
content_type: ct,
)
# attach duplicated profile to new_user
new_user.profile.attach(new_blob)
# FAILING TEST: most recent attachment's record_id will NOT match new_user.id
assert_equal new_user.id, ActiveStorage::Attachment.last.record_id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment