diff --git a/lib/ransack/adapters/active_record/context.rb b/lib/ransack/adapters/active_record/context.rb index 465b3a8..76a3008 100644 --- a/lib/ransack/adapters/active_record/context.rb +++ b/lib/ransack/adapters/active_record/context.rb @@ -27,7 +27,7 @@ module Ransack return nil unless attr && attr.valid? name = attr.arel_attribute.name.to_s table = attr.arel_attribute.relation.table_name - schema_cache = ::ActiveRecord::Base.connection.schema_cache + schema_cache = self.klass.connection.schema_cache unless schema_cache.send(database_table_exists?, table) raise "No table named #{table} exists." end @@ -290,7 +290,7 @@ module Ransack join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 end else - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, relation.table.name, join_list) + alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, relation.table.name, join_list) join_dependency = JoinDependency.new(relation.klass, relation.table, association_joins, alias_tracker) join_nodes.each do |join| join_dependency.send(:alias_tracker).aliases[join.left.name.downcase] = 1 @@ -333,7 +333,7 @@ module Ransack ) found_association = jd.join_root.children.last else - alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(::ActiveRecord::Base.connection, parent.table.name, []) + alias_tracker = ::ActiveRecord::Associations::AliasTracker.create(self.klass.connection, parent.table.name, []) jd = JoinDependency.new( parent.base_klass, parent.base_klass.arel_table, diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 4a0d5e7..bfa444e 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -18,6 +18,13 @@ module Ransack expect(subject.object).to be_an ::ActiveRecord::Relation end + context "multiple database connection" do + it "does not raise error" do + expect { Person.ransack(name_cont: "test") }.not_to raise_error + expect { SubDB::OperationHistory.ransack(people_id_eq: 1) }.not_to raise_error + end + end + context 'with scopes' do before do allow(Person) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ebedf2a..ab3b430 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -37,6 +37,7 @@ RSpec.configure do |config| line = '=' * message.length puts line, message, line Schema.create + SubDB::Schema.create end config.before(:all) { Sham.reset(:before_all) } diff --git a/spec/support/schema.rb b/spec/support/schema.rb index 88363d2..48f22ca 100644 --- a/spec/support/schema.rb +++ b/spec/support/schema.rb @@ -261,3 +261,30 @@ module Schema ) end end + +module SubDB + class Base < ActiveRecord::Base + self.abstract_class = true + establish_connection( + adapter: 'sqlite3', + database: ':memory:' + ) + end + + class OperationHistory < Base + end + + module Schema + def self.create + s = ::ActiveRecord::Schema.new + s.instance_variable_set(:@connection, SubDB::Base.connection) + s.verbose = false + s.define({}) do + create_table :operation_histories, force: true do |t| + t.string :operation_type + t.integer :people_id + end + end + end + end +end