代码之家  ›  专栏  ›  技术社区  ›  Peter

在Rails中插入超类,没有相应的数据库表

  •  1
  • Peter  · 技术社区  · 15 年前

    我想在Ruby中为两个模型提供一个通用的超类,共享几种方法。假设我想要一个 Truck Car 继承 Vehicle . 以下是一些选项:

    1. 制造 class Vehicle < ActiveRecord::Base 并拥有 class Truck < Vehicle 等等,但是我说我没有桌子 车辆 (我也不想要)。

    2. 使用 module Vehicle include Vehicle 在里面 class Truck < ActiveRecord::Base . 但是然后 attr_reader 而朋友们不会被应用到 卡车 .

    因此,我想要 class Vehicle . 不需要桌子怎么做?我相信有一个标准的,很好的方法来做这件事…

    1 回复  |  直到 15 年前
        1
  •  4
  •   khelll    15 年前

    添加一个 方法称为 abstract_class? 它会回来 true :

    class Vehicle < ActiveRecord::Base
      def self.abstract_class?
        true
      end
    end