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.
相关问题
- Eager-loading association count with Arel (Rails 3
- Rails simple model attributes not saved to databas
- jquery-ui progressbar not showing
- PDF attachment in email is called 'Noname'
- mongoid polymorphic association error
相关文章
- “No explicit conversion of Symbol into String” for
- Rspec controller error expecting <“index”> but
- Factory_girl has_one relation with validates_prese
- Rails: Twitter Bootstrap Buttons when visited get
- is there a “rails” way to redirect if mobile brows
- Got ActiveRecord::AssociationTypeMismatch on model
- superclass mismatch for class CommentsController (
- rails 3, how add a simple confirmation dialog when
If your model classes are already loaded then you could list them by finding all the classes that include the Mongoid::Document module.
or if you just want the class names:
If you need to force your models to load before running this you can do so like this (in Rails 3):
(assuming all of your models are in
app/models
or a sub-directory)The problem with
Mongoid.models
is that apparently only returns the already loaded models. I did the following experiment in the rails console (I have three models:Admin
,User
andDevice
):But if I instantiate the class
Device
and then call the same method, I get a different result:So this could represent a problem, specially if the method is called from a rake task. The Chris' solution works fine so I guess that is the best option at this moment :S (I can't got working the Steve's solution with Rails 4).
You can do this in Mongoid version 3.1 and higher:
Mongoid.models
If you are in Rails' development mode where the models are not automatically loaded, run
Rails.application.eager_load!
to load the entire application.Here is a gist I coded to get all Mongoid models, and optionally filter them by superclass (say, if you want to only get models inheriting from a specific class).
https://gist.github.com/4633211
(compared to Steve solution, it's also working with namespaced models)