thoughtbot--shoulda-matchers/lib/shoulda/matchers
Mauro George c0a1578435 Fail have_many :through when inverse is invalid
When used with a `:through` association where a corresponding
`belongs_to` association has not been set up on the inverse model, the
`have_many` matcher raises this error:

    Failures:

      1) User should have many projects through clients
         Failure/Error: it {  should have_many(:projects).through :clients }
         NoMethodError:
           undefined method `class_name' for nil:NilClass
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:871:in `derive_class_name'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/reflection.rb:147:in `class_name'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:1067:in `rescue in class_exists?'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:1064:in `class_exists?'
         # /Users/<USER>/.rvm/gems/ruby-2.2.0/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_record/association_matcher.rb:928:in `matches?'
         # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

Fortunately, ActiveRecord has a `check_validity!` method we can call on
an association. For `:through` associations, this will run through a
litany of checks, one of which is to check for the inverse association,
which we want in this case. We rescue the error that is produced and
include this in the failure message.

Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
2020-07-10 22:44:22 -06:00
..
action_controller
active_model Update validate_exclusion_of_matcher.rb 2020-01-17 08:53:30 -07:00
active_record Fail have_many :through when inverse is invalid 2020-07-10 22:44:22 -06:00
doublespeak
independent
integrations Update messaging around presence + belongs_to 2019-06-07 21:46:05 -06:00
util Clear up confusion w/ presence around belongs_to associations 2019-06-01 17:43:13 -06:00
action_controller.rb Update documentation 2019-02-16 03:01:35 -07:00
active_model.rb Update documentation 2019-02-16 03:01:35 -07:00
active_record.rb Add have_rich_text_matcher 2020-01-23 09:29:57 -07:00
configuration.rb Update messaging around presence + belongs_to 2019-06-07 21:46:05 -06:00
doublespeak.rb
error.rb
independent.rb Update documentation 2019-02-16 03:01:35 -07:00
integrations.rb
matcher_context.rb
rails_shim.rb Remove code for unsupported versions (#1270) 2020-01-01 19:10:35 -08:00
routing.rb
util.rb Truncate long values in descriptions. Fixes #1041 2020-05-06 09:29:20 -06:00
version.rb Release version 4.3.0 (#1276) 2020-02-17 23:23:52 -08:00
warn.rb Update documentation 2019-02-16 03:01:35 -07:00