ame*_*ini 11 ruby ruby-on-rails
在我正在研究的Rails应用程序上,我有一个与单表继承模型"Node"相关的模型"Type":Node的任何可能的子类都被定义为types表中的Type.
现在可以在初始化程序中加载所有类,但我只想在需要时加载子类.
我能想到的最好的解决方案是对未初始化的常量进行回退,该常量将检查该常量是否可以表示应用程序中的类,类似于method_missing的类.
我想对如何以及在何处定义此逻辑提出一些建议,或者是否有更好的解决方案.
Pet*_*ton 16
我不知道这是否是新的,但我认为值得补充.可以使用缺少方法作为类方法
class Example
def method_missing(method_name, *arguments, &block)
puts 'needs this instance method'
end
def self.method_missing(method_name, *arguments, &block)
puts 'needs this class method'
end
end
Run Code Online (Sandbox Code Playgroud)
Mic*_*ohl 14
有Module#const_missing:
http://apidock.com/ruby/Module/const_missing
我假设您可以(ab)使用它来满足您的需求.
| 归档时间: |
|
| 查看次数: |
5312 次 |
| 最近记录: |