diff --git a/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb index eaef3fcd..e4adb722 100644 --- a/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb @@ -47,13 +47,17 @@ module Shoulda # :nodoc: end def collection? - if @subject.class.respond_to?(:reflect_on_association) && - reflection = @subject.class.reflect_on_association(@attribute) + if reflection [:has_many, :has_and_belongs_to_many].include?(reflection.macro) else false end end + + def reflection + @subject.class.respond_to?(:reflect_on_association) && + @subject.class.reflect_on_association(@attribute) + end end end diff --git a/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb index ceb16c97..de45106c 100644 --- a/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +++ b/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do - context "a required attribute" do before do define_model :example, :attr => :string do