本文介绍了得到所有车型,从轨道列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要所有型号(class_names),其中有模式魔方在最后的清单。
I need a list with all models (class_names) which have the pattern "Cube" at the end.
例如:
我所有的车型:ModelFoo,ModelBar,ModelBarCube,Mode2BarCube
all my models:ModelFoo, ModelBar, ModelBarCube, Mode2BarCube
我所需要的:
['ModelBarCube','Mode2BarCube']
['ModelBarCube', 'Mode2BarCube']
推荐答案
由于Rails没有加载类,除非它需要他们,你必须从文件夹中读取模型。这里是code
Since Rails doesn't load classes unless it needs them, you must read the models from the folder. Here is the code
Dir.glob(Rails.root + '/app/models/*.rb').each { |file| require file }
@models = Object.subclasses_of(ActiveRecord::Base).select { |model|
model.name[-4..-1] == "Cube"
}
这篇关于得到所有车型,从轨道列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!