Blog

Inspect all Models in a Rails application

Placeholder Avatar
Ben Wiseley
July 30, 2012

Quick Tip: Find all models in a project that include a certain method. Useful for some engine applications. When you don’t know what the parent application has you can do things like:

# if cache_classes is on - you need to do this # call first - otherwise you'll only get models # that have been touched/initialized Rails.application.eager_load!

models = ActiveRecord::Base.descendants

find all models that have :users

models.select {|m| m.new.respond_to?(:users) } </code>