Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: Configuring Active Record to Use Different Base Classes for Sharded and Non-Sharded Models #1764

Open
rishav-enigma opened this issue Feb 7, 2025 · 0 comments

Comments

@rishav-enigma
Copy link

Background
In our legacy Rails application, we have been using the active_record_shards gem to handle database sharding. Prior to upgrading to Rails 7.2, we patched certain models (e.g., Doorkeeper models) using not_sharded, ensuring they always connected to the primary database:

Doorkeeper::Application.class_eval do
  not_sharded
  ...
end

With the upgrade, we introduced two base classes to handle this explicitly:

class GlobalRecord < ActiveRecord::Base
  self.abstract_class = true
  connects_to database: { writing: :primary }
end
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  ...
  # Defaults to shards
end

Problem
Instead of manually replacing every model’s superclass (e.g., Doorkeeper::Application < GlobalRecord), we are looking for a configuration-based solution that allows certain models to automatically connect to the primary database while keeping the rest on sharded connections.

Is there a recommended approach in Rails 7.2 to handle this use case without modifying every model explicitly?

Expected Behavior
A way to configure specific models (e.g., Doorkeeper::Application) to always use GlobalRecord or connect to :primary without manually changing every occurrence.
Ideally, this would be a simple Active Record configuration instead of modifying all model definitions.
Any guidance or best practices for handling this in Rails 7.2 would be greatly appreciated.

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

No branches or pull requests

1 participant