diff --git a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb index b50939c7..cf933740 100644 --- a/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +++ b/lib/shoulda/matchers/active_record/have_db_index_matcher.rb @@ -160,7 +160,7 @@ module Shoulda end def indexes - ::ActiveRecord::Base.connection.indexes(table_name) + model_class.connection.indexes(table_name) end def expectation diff --git a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb index 57bfb601..4e2a8b65 100644 --- a/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb @@ -3,7 +3,8 @@ require 'unit_spec_helper' describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do context 'have_db_index' do it 'accepts an existing index' do - expect(with_index_on(:age)).to have_db_index(:age) + expect(with_index_on(:age1, parent_class: DevelopmentRecord)).to have_db_index(:age1) + expect(with_index_on(:age2, parent_class: ProductionRecord)).to have_db_index(:age2) end it 'rejects a nonexistent index' do @@ -77,9 +78,11 @@ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher, type: :model do end def with_index_on(column_name, index_options = {}) - create_table 'employees' do |table| + parent_class = index_options.delete(:parent_class) || ActiveRecord::Base + + create_table('employees', connection: parent_class.connection) do |table| table.integer column_name end.add_index(:employees, column_name, index_options) - define_model_class('Employee').new + define_model_class('Employee', parent_class: parent_class).new end end