Is there a way that you can get a collection of all of the Models in your Rails app?
Basically, can I do the likes of: -
Models.each do |model|
puts model.class.name
end
Is there a way that you can get a collection of all of the Models in your Rails app?
Basically, can I do the likes of: -
Models.each do |model|
puts model.class.name
end
Just in case anyone stumbles on this one, I've got another solution, not relying on dir reading or extending the Class class...
This will return an array of classes. So you can then do
I think @hnovick's solution is a cool one if you dont have table-less models. This solution would work in development mode as well
My approach is subtly different though -
classify is well supposed to give you the name of the class from a string properly. safe_constantize ensures that you can turn it into a class safely without throwing an exception. This is needed in case you have database tables which are not models. compact so that any nils in the enumeration are removed.
Yes there are many ways you can find all model names but what I did in my gem model_info is , it will give you all the models even included in the gems.
then simply print this
can check this
will return
Additional information If you want to call a method on the object name without model:string unknown method or variable errors use this
I looked for ways to do this and ended up choosing this way:
source: http://portfo.li/rails/348561-how-can-one-list-all-database-tables-from-one-project