【发布时间】:2011-07-17 17:38:16
【问题描述】:
我目前对将descendants 用于 ActiveRecord::Base-Objects 感到非常困惑。
我浏览了互联网并测试了所有解决方案,但没有一个适合我的需求。
我想要做的:获取一个包含所有 ActiveRecord::Base 子类的数组,包括这些子类的子类,例如
Entity < ActiveRecord::Base
ChildEntity < Entity
Property < ActiveRecord::Base
我当前的问题: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
出于调试目的,我只是打印出来。 我正在使用 Rails 3。
我认为问题一定出在我的代码上。我正在使用<% all_entities %>调用当前视图中的方法
感谢您的帮助。
【问题讨论】:
-
如我所见,ActiveRecord::Base.descendants 应该列出所有后代(甚至是子子类)。但无论如何它不会收集所有后代 - 最多 4 个实体 - 我的模型有 7 个
标签: ruby-on-rails-3 list inheritance models descendant