Get a list of all ActiveRecord::Base descendants that have a count > 0

Donnerstag, 19. Juli 2018, 13:43 Uhr | roberto@vasquez-angel.de |

This is useful for testing, when you want to know which models have changed their counts:

ActiveRecord::Base.descendants.each_with_object({}) { |o,m| m[o.name] = o.count }.sort { |o, c| o[1] <=> c[1] }.reject { |c| c[1] < 1 }

If you want to omit certain classes, you may reject them like this:

ActiveRecord::Base.descendants.reject { |e| e.to_s =~/(ApplicationRecord|ActiveStorage).*/ }.each_with_object({}) { |o,m| m[o.name] = o.count }.sort { |o, c| o[1] <=> c[1] }.reject { |c| c[1] < 1 }