views:

491

answers:

1

Hi

I need a list with all models (class_names) which have the pattern "Cube" at the end.

example:

all my models: ModelFoo, ModelBar, ModelBarCube, Mode2BarCube

what I need:

['ModelBarCube', 'Mode2BarCube']

+7  A: 

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"
  }
Senad Uka