#57042·rails

ActiveStorage dynamic service can't access association when using .build

Author: MarcelEekenCreated Mar 23, 2026Updated Sep 28, 2026

Steps to reproduce

ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"

  gem "sqlite3"
end

require "active_record/railtie"
require "active_storage/engine"
require "minitest/autorun"

ENV["DATABASE_URL"] = "sqlite3::memory:"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f

  config.root = __dir__
  config.hosts << "example.org"
  config.eager_load = false
  config.session_store :cookie_store, key: "cookie_store_key"
  config.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger  = config.logger

  config.active_storage.service = :eu_storage
  config.active_storage.service_configurations = {
    eu_storage: {
      root: Dir.tmpdir,
      service: "Disk"
    },
    us_storage: {
      root: Dir.tmpdir,
      service: "Disk"
    }
  }

  config.active_job.queue_adapter = :inline
end
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 do |t|
    t.string "location"
  end
  create_table :attachments, force: true do |t|
    t.integer "user_id"
  end
end

class User < ActiveRecord::Base
  has_many :attachments, inverse_of: :user

  enum :location, eu: :eu, us: :us
end

class Attachment < ActiveRecord::Base
  belongs_to :user, inverse_of: :attachments

  has_one_attached :file, service: ->(record) { record.storage_service_name }

  def storage_service_name
    user&.eu? ? :eu_storage : :us_storage
  end
end

class BugTest < ActiveSupport::TestCase
  def test_dynamic_service_with_association
    user = User.create!(location: :eu)

    # Working examples
    working_example = user.attachments.build
    working_example.assign_attributes(
      file: {
        content_type: "text/plain",
        filename: "dummy.txt",
        io: ::StringIO.new("dummy"),
      }
    )
    assert_equal working_example.file.service.name, :eu_storage

    working_example2 = Attachment.create!(
      user_id: user.id,
      file: {
        content_type: "text/plain",
        filename: "dummy.txt",
        io: ::StringIO.new("dummy"),
      }
    )
    assert_equal working_example2.file.service.name, :eu_storage

    # Failing examples
    failing_example = user.attachments.build(
      file: {
        content_type: "text/plain",
        filename: "dummy.txt",
        io: ::StringIO.new("dummy"),
      }
    )

    assert_equal failing_example.file.service.name, :eu_storage
  end
end

Expected behavior

When using a dynamic service definition in an attachment, it should be possible to determine the service based on an association when creating an instance of the class with the attachment using .build on the association.

Actual behavior

When using .build on a assocation with the uploaded file directly in the attributes passed to .build, the service can't be correctly determined due to the association being set after the attributes are set.

System configuration

Rails version: 8.1.2

Ruby version: 4.0.1