Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepts association with :inverse_of set to false and non standard foreign key #1106

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down