Rails:在模型中创建子文件夹?
我将有大量的子类,因此想将它们组织在一个名为“流”的子文件夹下。我将以下行添加到environment.rb,以便加载子文件夹中的所有类:
Rails::Initializer.run do |config|
...
config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? }
...
end
我认为这可以解决按照惯例将模型类命名为相应模块的问题。但是,当我尝试调用流文件夹中名为 Stream 的类之一时,出现以下错误:
NoMethodError: undefined method `new' for Stream:Module
from (irb):28
from /usr/local/bin/irb:12:in `<main>'
这是父级和一个子级的模型:
class Stream
end
class EventStream < Stream
end
知道问题是什么吗?
I'm going to have a ton of subclasses, so want to organize them under a subfolder called stream. I added the following line to the environment.rb so that all classes in the subfolder would be loaded:
Rails::Initializer.run do |config|
...
config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? }
...
end
I thought this would solve the issue in which by convention the model class is namespaced into an according module. However, when I try to call one of the classes called stream in the stream folder, I get the following error:
NoMethodError: undefined method `new' for Stream:Module
from (irb):28
from /usr/local/bin/irb:12:in `<main>'
Here's the model for the parent and one child:
class Stream
end
class EventStream < Stream
end
Any idea what the issue is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论