gregnavis / active_record_doctor

Identify database issues before they hit production.
MIT License
1.76k stars 55 forks source link

ArgumentError: Polymorphic associations do not support computing the class. #95

Closed barnaclebarnes closed 2 years ago

barnaclebarnes commented 2 years ago

I have a Polymorphic belongs_to like this:

belongs_to :subtype, polymorphic: true, dependent: :destroy, autosave: true

I get this error when running the rake task:

ArgumentError: Polymorphic associations do not support computing the class.
/usr/local/bundle/gems/activerecord-6.1.4.7/lib/active_record/reflection.rb:407:in `compute_class'
/usr/local/bundle/gems/activerecord-6.1.4.7/lib/active_record/reflection.rb:366:in `klass'
/usr/local/bundle/gems/active_record_doctor-1.9.0/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:45:in `block (2 levels) in detect'
/usr/local/bundle/gems/active_record_doctor-1.9.0/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:42:in `each'
/usr/local/bundle/gems/active_record_doctor-1.9.0/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:42:in `block in detect'
/usr/local/bundle/gems/active_record_doctor-1.9.0/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:35:in `each'
/usr/local/bundle/gems/active_record_doctor-1.9.0/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:35:in `detect'

I guess a) it shouldn't error out and b) should give some advice on what to do.

gregnavis commented 2 years ago

Thank you for opening the issue, @barnaclebarnes. I added support for polymorphic associations so expect to see an update later today.

barnaclebarnes commented 2 years ago

@gregnavis Thanks for that. I just pulled down master and it is now crashing on a different error. I have this:

class Map < ApplicationRecord
  has_many :screen_section_locations, inverse_of: :map
end

class StorySectionLocation < ApplicationRecord
  belongs_to :map, inverse_of: :screen_section_locations, optional: true
end

And this is causing this on when processing the Map model:

NameError: uninitialized constant Map::ScreenSectionLocation
/usr/local/bundle/gems/activerecord-6.1.4.7/lib/active_record/inheritance.rb:237:in `compute_type'
/usr/local/bundle/gems/activerecord-6.1.4.7/lib/active_record/reflection.rb:409:in `compute_class'
/usr/local/bundle/gems/activerecord-6.1.4.7/lib/active_record/reflection.rb:366:in `klass'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:129:in `map'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:129:in `dependent_models'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:101:in `deletable?'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:61:in `block (3 levels) in detect'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:61:in `each'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:61:in `partition'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:61:in `block (2 levels) in detect'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:51:in `each'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:51:in `block in detect'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:44:in `each'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/incorrect_dependent_option.rb:44:in `detect'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/base.rb:50:in `run'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/detectors/base.rb:17:in `run'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/runner.rb:15:in `block in run_one'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/errors.rb:5:in `handle_exception'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/runner.rb:14:in `run_one'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/runner.rb:26:in `block in run_all'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/runner.rb:25:in `each'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/runner.rb:25:in `run_all'
/usr/local/bundle/bundler/gems/active_record_doctor-cbb9183887f1/lib/active_record_doctor/rake/task.rb:60:in `block in define'
/usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
Tasks: TOP => active_record_doctor
(See full trace by running task with --trace)

Thoughts?

gregnavis commented 2 years ago

Looks like a bug in the app itself. Notice that Map defines screen_section_locations (with emphasis on screen) and the other class is named StorySectionLocation (with emphasis on Story).

barnaclebarnes commented 2 years ago

@gregnavis LOL, you're right. All works now. 🥇