列出从ActiveRecord :: Base开始的继承树中的所有模型

1 inheritance list descendant models ruby-on-rails-3

我目前对使用descendantsActiveRecord :: Base-Objects 非常困惑.我浏览过互联网并测试了所有解决方案,但它们都不符合我的需求.

我想做什么:获取ActiveRecord :: Base的所有子类的数组,包括这些子类的子类,例如

Entity < ActiveRecord::Base
ChildEntity < Entity

Property < ActiveRecord::Base
Run Code Online (Sandbox Code Playgroud)

我当前的问题:ActiveRecord :: Base.descendants没有列出从ActiveRecord :: Base继承的所有类.也许错误就在我身边:这是我的代码.

 def all_entities
   rec_all_entities(ActiveRecord::Base)
 end

 def rec_all_entities(motherEntity)
    logger.debug("mother: " + motherEntity.to_s + " descendants: " + motherEntity.descendants.to_s)
    motherEntity.descendants.each do |childEntity|
      rec_all_entities(childEntity)
    end
 end  
Run Code Online (Sandbox Code Playgroud)

对于debbuging目的,我只是打印出来.我正在使用Rails 3.

我认为错误必须在我的代码中.我正在调用当前正在视图中的方法<% all_entities %>

谢谢你的帮助.

Rub*_*ils 7

这对我很有用.另外,有没有办法在你的Rails应用程序中获得所有模型的集合?

> Rails.application.eager_load!
> ActiveRecord::Base.descendants
Run Code Online (Sandbox Code Playgroud)