0

The method to walk through apps/models and look into each file for classes is not what I'm looking for

mcmlxxxiii
  • 913
  • 1
  • 9
  • 21
  • The difference is that not all models might be loaded. Some might have failed for some reason, though not having failed a project in general. – mcmlxxxiii Dec 28 '09 at 20:27
  • Can't you just try to load them then as you walk through? – MattMcKnight Dec 29 '09 at 05:24
  • I faced a strange case during a project's runtime which is in fact that some models seemed not to be loaded. For now, I've solved my problem. But first I wanted to investigate it and I needed a list of loaded models for that. – mcmlxxxiii Dec 30 '09 at 01:22

1 Answers1

0

Yes you can: Is there a way to get a collection of all the Models in your Rails app?

For example:

Module.constants.select do |constant_name|
  constant = eval constant_name
  if not constant.nil? and constant.is_a? Class and constant.superclass == ActiveRecord::Base
    constant
  end
end
Community
  • 1
  • 1
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
  • There's no such a way that I asked for among listed in there. Some do work properly, some don't. And no suitable method for my case. – mcmlxxxiii Dec 28 '09 at 20:37
  • This method most of all looks like a needed answer. But when I tried it on my project, it returned just a few of its models. I did not dive deep into why that's so. – mcmlxxxiii Dec 29 '09 at 19:30
  • It could be that you actually have to instantiate the model and that would be a bummer. Can't test that theory right now. – Jonas Elfström Dec 29 '09 at 22:05
  • This method also returns just a couple of models either from within a console or from within a view. Even more, it returns different sets of models from different views. That means that you're right -- the returned models list depends on context. The question turned out to be not so simple... – mcmlxxxiii Dec 30 '09 at 01:16