diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 527dc57d5..c86f1803c 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -1366,7 +1366,7 @@ def foreign_key end def foreign_key_reflection - if [:has_one, :has_many].include?(macro) && reflection.options.include?(:inverse_of) + if [:has_one, :has_many].include?(macro) && reflection.options.include?(:inverse_of) && reflection.options[:inverse_of] != false associated_class.reflect_on_association(reflection.options[:inverse_of]) else reflection diff --git a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb index 361e011c3..7c046d7cf 100644 --- a/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb @@ -870,6 +870,17 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) expect(Parent.new).not_to have_many(:children) end + it 'accepts an association with a nonstandard foreign key, with reverse association turned off' do + define_model :child, ancestor_id: :integer do + end + + define_model :parent do + has_many :children, foreign_key: :ancestor_id, inverse_of: false + end + + expect(Parent.new).to have_many(:children) + end + def having_many_children(options = {}) define_model :child, parent_id: :integer define_model(:parent).tap do |model|