diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 911d8e27d..032009dc2 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -240,7 +240,15 @@ def associated_class end def foreign_key - reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name + fk_reflection = reflection + if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of) + fk_reflection = associated_class.reflect_on_association( + reflection.options[:inverse_of] + ) + end + fk_reflection.respond_to?(:foreign_key) ? + fk_reflection.foreign_key : + fk_reflection.primary_key_name end def through?