Skip to content

Commit

Permalink
accepts an association with a nonstandard foreign key, with reverse a…
Browse files Browse the repository at this point in the history
…ssociation turned off
  • Loading branch information
Savater Sebastien authored and mcmire committed Nov 27, 2019
1 parent 5b8875e commit 65e13ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,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|
Expand Down

0 comments on commit 65e13ae

Please sign in to comment.