How to get an array of subclasses in Rails

I have a model object that subclasses ActiveRecord. Also, using STI, I defined subclasses of this object that define different types and behavior. The structure looks something like this:

class AppModule < ActiveRecord::Base
  belongs_to :app 
end

class AppModuleList < AppModule

end

class AppModuleSearch < AppModule

end

class AppModuleThumbs < AppModule

end

Now that the user has the ability to create new AppModules, I would like them to choose from the drop-down menu. However, I was not able to get a list of subclasses of AppModule using the subclasses () method:

<% form_for(@app_module) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :type %><br />
    <%= f.select(:type, options_from_collection_for_select(@app_module.subclasses().map{ |c| c.to_s }.sort)) %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

I get:

NoMethodError: undefined method `subclasses' for #<AppModule:0x1036b76d8>

I would appreciate any help. Many thanks!

+5
source share
2 answers

, subclasses .. ( , , , -, , ). RoR , subclasses Class#inherited ( , RoR descendents_tracker).

+10

AppModule.descendants.map &:name - , . :

<% form_for(@app_module) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :type %><br />
    <%= f.select(:type, options_from_collection_for_select(AppModule.descendants.map(&:name).sort)) %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %> 
+5

All Articles