accepts an association with a nonstandard foreign key, with reverse association turned off

This commit is contained in:
Savater Sebastien 2018-06-05 16:10:53 +02:00 committed by Elliot Winkler
parent 5b8875e494
commit 65e13ae040
2 changed files with 12 additions and 1 deletions

View File

@ -1394,7 +1394,7 @@ module Shoulda
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

View File

@ -1144,6 +1144,17 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher, type: :model do
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|