This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
look up ActiveRecord::Base.descendants instead of .subclasses #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backstory
In Rails 5, all models inherit from
ApplicationRecord
, which inherits fromActiveRecord::Base
( rails/rails#22567 ). For instance, my User model:sprig-reap
looks up models viaActiveRecord::Base.subclasses
, which only looks up direct subclasses ofActiveRecord::Base
. As a result,rake db:seed:reap
in Rails 5 fails silently with no records reaped:Reaping from the Rails console shows the error:
Change
Using the
.descendants
method, however, all levels of subclassing can be accessed, and I can successfully reap all of my models.Rather than changing sprig-reap to look up direct subclasses of
ApplicationRecord
, I've modified it to callActiveRecord::Base.descendants
. This should preserve compatibility with pre-Rails-5 models that inherit directly fromActiveRecord:Base
and/or don't inherit fromApplicationRecord
.