From 872e9f64139b7f187e9b80a91020a5cac18bf23d Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 17 Jul 2026 09:13:10 -0400 Subject: [PATCH] Delegate Search connection to `@scoped_resource` 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. --- lib/administrate/search.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/administrate/search.rb b/lib/administrate/search.rb index 5619da249..975583f0d 100644 --- a/lib/administrate/search.rb +++ b/lib/administrate/search.rb @@ -137,15 +137,15 @@ def query_table_name(attr) else @scoped_resource.reflect_on_association(attr).klass.table_name end - ActiveRecord::Base.connection.quote_table_name(unquoted_table_name) + connection.quote_table_name(unquoted_table_name) else - ActiveRecord::Base.connection + connection .quote_table_name(@scoped_resource.table_name) end end def column_to_query(attr) - ActiveRecord::Base.connection.quote_column_name(attr) + connection.quote_column_name(attr) end def tables_to_join @@ -162,6 +162,14 @@ def term query.terms end + def connection + if @scoped_resource.respond_to?(:connection) + @scoped_resource.connection + else + ActiveRecord::Base + end + end + attr_reader :resolver, :query end end