Find all the mongoid model names in my application

mongoid, ruby-on-rails-3

Solution

If your model classes are already loaded then you could list them by finding all the classes that include the Mongoid::Document module.

Object.constants.collect { |sym| Object.const_get(sym) }.
  select { |constant| constant.class == Class && constant.include?(Mongoid::Document) }

or if you just want the class names:

Object.constants.collect { |sym| Object.const_get(sym) }.
  select { |constant| constant.class == Class && constant.include?(Mongoid::Document) }.
  collect { |klass| klass.name }

If you need to force your models to load before running this you can do so like this (in Rails 3):

Dir["#{Rails.root}/app/models/**/*.rb"].each { |path| require path }

(assuming all of your models are in `app/models` or a sub-directory)

Problem

Is there a way to find out all the Mongoid Models names in my rails app. I can find all the models just by getting all the file inside my app/models folder but i specifically want mongoid model names.

Original source