close
Skip to content

Delegate Search connection to @scoped_resource#3049

Open
seanpdoyle wants to merge 1 commit into
mainfrom
query-multi-database-support
Open

Delegate Search connection to @scoped_resource#3049
seanpdoyle wants to merge 1 commit into
mainfrom
query-multi-database-support

Conversation

@seanpdoyle

Copy link
Copy Markdown
Contributor

When an Administrate::Search::Query instance is constructed for a resource class that connects to a different database than the primary, connections scoped by ActiveRecord::Base fail to connect to the database specified by the class that descends from ActiveRecord::Base and declares its own connect_to.

This commit modifies the Administrate::Search::Query class to access the connection through the @scoped_resources Active Record Relation instance when possible, falling back to ActiveRecord::Base when not possible.

@seanpdoyle
seanpdoyle requested a review from nickcharlton July 17, 2026 13:17
@seanpdoyle
seanpdoyle force-pushed the query-multi-database-support branch from e7d1713 to ecff9e3 Compare July 20, 2026 14:07
@seanpdoyle
seanpdoyle changed the base branch from main to bundler-audit July 20, 2026 14:07
Base automatically changed from bundler-audit to main July 20, 2026 14:35
When an `Administrate::Search::Query` instance is constructed for a
resource class that connects to a different database than the primary,
connections scoped by `ActiveRecord::Base` fail to connect to the
database specified by the class that descends from `ActiveRecord::Base`
and declares its own `connect_to`.

This commit modifies the `Administrate::Search::Query` class to access
the connection through the `@scoped_resources` Active Record Relation
instance when possible, falling back to `ActiveRecord::Base` when not
possible.
@seanpdoyle
seanpdoyle force-pushed the query-multi-database-support branch from ecff9e3 to 872e9f6 Compare July 20, 2026 14:35
@seanpdoyle
seanpdoyle requested a review from pablobm July 20, 2026 14:35
Comment on lines +166 to +170
if @scoped_resource.respond_to?(:connection)
@scoped_resource.connection
else
ActiveRecord::Base
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the test suite with the following change still fully passes:

      if @scoped_resource.respond_to?(:connection)
        @scoped_resource.connection
      else
-       ActiveRecord::Base
+       fail
      end

@nickcharlton @pablobm to me, that signals that the @scoped_resource will always be an ActiveRecord::Relation or ActiveRecord::Base-descended class. Conceptually, is that true?

If so, I'm happy to remove the else branch of this conditional and inline the #connection calls back into the original call sites.

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