Skip to content

Conversation

@keshavbiswa
Copy link

@keshavbiswa keshavbiswa commented Nov 13, 2025

Problem

I have a PrimaryRecord (untenanted) and ApplicationRecord(tenanted) and I need ActiveStorage to work on both untenanted records and tenanted records. Currently it only works on tenanted.

Here's an example .

class PrimaryRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary }
end
class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class

  tenanted "secondary"
end
class Organization < PrimaryRecord
  has_one_attached :logo
end
class User < ApplicationRecord
  has_one_attached :photo
end

So for tenanted works fine.

Defaulting current tenant to "development-tenant"
app(dev):001> User.first.photo
  SCHEMA [tenant=development-tenant] (1.6ms)  SELECT sqlite_version(*) /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA foreign_keys = ON /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA journal_size_limit = 67108864 /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA cache_size = 2000 /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA journal_mode = WAL /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA synchronous = NORMAL /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  PRAGMA mmap_size = 134217728 /*application='App'*/
  SCHEMA [tenant=development-tenant] (2.3ms)  SELECT name FROM pragma_table_list WHERE schema <> 'temp' AND name NOT IN ('sqlite_sequence', 'sqlite_schema') AND name = 'schema_migrations' AND type IN ('table') /*application='App'*/
  SCHEMA [tenant=development-tenant] (0.0ms)  SELECT name FROM pragma_table_list WHERE schema <> 'temp' AND name NOT IN ('sqlite_sequence', 'sqlite_schema') AND name = 'ar_internal_metadata' AND type IN ('table') /*application='App'*/
  ActiveRecord::SchemaMigration Load [tenant=development-tenant] (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC /*application='App'*/
  User Load [tenant=development-tenant] (0.1ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 /*application='App'*/
(app):1:in '<main>': undefined method 'photo' for nil (NoMethodError)
app(dev):002>

But for untenanted raises ActiveRecord::Tenanted::TenantDoesNotExistError

Loading development environment (Rails 8.2.0.alpha)
Type 'help' for help.
Defaulting current tenant to "development-tenant"
app(dev):001> ApplicationRecord.current_tenant = nil
=> nil
app(dev):002> Organization.first.logo
  Organization Load (0.1ms)  SELECT "organizations".* FROM "organizations" ORDER BY "organizations"."id" ASC LIMIT 1 /*application='App'*/
An error occurred when inspecting the object: #<ActiveRecord::Tenanted::TenantDoesNotExistError: The database for tenant "" does not exist.>
=> #<ActiveStorage::Attached::One:0x0000000127034dc8
app(dev):003>

Solution

This PR introduces config.active_record_tenanted.allow_untenanted_active_storage which by default is false. But if set to true will allow untenanted access to primary DBs as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant