Skip to content

Commit

Permalink
association_matcher foreign_key refinement
Browse files Browse the repository at this point in the history
#66

has_many and has_one tests failed with the reverse association has a
nonstandard name, (eg "belongs_to :author, :class_name => :User)

corrected by having have_one and have_many association tests check for an
:inverse_of on their association, and use the inverse relationship's
foreign key info if it can be found
  • Loading branch information
bloy committed Mar 23, 2012
1 parent 963a293 commit c863f7e
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -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?
Expand Down

0 comments on commit c863f7e

Please sign in to comment.