繁体   English   中英

无法理解Rails中的基类

[英]can't understand what Base class is in rails

我有一个类型的代码

Module Themes
  class NewTheme < Themes::Base
    def1
    end

    def2
    end
  end
end

当我尝试使用Themes :: NewTheme.instance创建新主题的实例时,该实例为空,这怎么了? 还有什么是:: Base? 我在任何地方都找不到清晰度

您应该说NewTheme.new而不是Themes::NewTheme来实例化您的课程。

对于基类,它只是“最终”父类的一种-没有父类,但有子类(也许只有几个)的类。 这是ActiveRecord :: Base的示例

class Song < ActiveRecord::Base
  # Uses an integer of seconds to hold the length of the song

  def length=(minutes)
    write_attribute(:length, minutes.to_i * 60)
  end

  def length
    read_attribute(:length) / 60
  end
end

在这种情况下, Song类将免费获取ActiveRecord :: Base中定义的所有方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM