Rails中如何获取子类数组

发布于 2024-10-02 07:46:55 字数 893 浏览 8 评论 0原文

我有一个模型对象,它是 ActiveRecord 的子类。此外,使用 STI,我定义了该对象的子类,它们定义了不同的类型和行为。结构看起来像这样:

class AppModule < ActiveRecord::Base
  belongs_to :app 
end

class AppModuleList < AppModule

end

class AppModuleSearch < AppModule

end

class AppModuleThumbs < AppModule

end

现在,在用户可以选择创建新 AppModule 的视图中,我希望他们从下拉菜单中进行选择。但是,我无法使用 subclasses() 方法获取 AppModule 的子类列表:

<% 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 %>

我得到:

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

我将不胜感激。多谢!

I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined subclasses of this object, which define different types and behaviors. 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, in a view where the user has the option to create new AppModules, I would like them to select from a dropdown menu. However I have not been 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'd appreciate any help. Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-10-09 07:46:55

看起来好像 subclasses 之类的最近添加(该方法存在于各种类上在不同的时间点,但不断被洗牌和删除;该链接似乎是该方法坚持的最早的点)。如果无法升级到最新版本的 RoR,您可以编写自己的子类并使用 Class#inherited (这就是 RoR 的 descendents_tracker 确实如此)。

It looks as though subclasses and the like is a recent addition (the method exists on various classes at various points in time, but kept getting shuffled around and removed; that link seems to be the earliest point that the method stuck around). If upgrading to the most recent version of RoR isn't an option, you can write your own subclasses and populate it using Class#inherited (which is what RoR's descendents_tracker does).

不知所踪 2024-10-09 07:46:55

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 %> 

AppModule.descendants.map &:name is what you're looking for. As in:

<% 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 %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文