我的应用程序中有几个表共享大部分列。我在一个模型中编写了所有验证,并尝试使所有其他模型从这个模型继承,但出现了ActiveRecord :: SubclassNotFound
错误。
以下是我的模型代码:
hospital.rb
class Hospital < ActiveRecord::Base
validates :cnes, presence: true, numericality: true
validates :name, presence: true, length: { maximum: 80 }
validates :address, presence: true, length: { maximum: 50 }
validates :neighborhood, presence: true, length: { maximum: 30 }
validates :phone, presence: true, length: { in: 10..25 }
validates :latitude, :longitude, presence: true, length: { maximum: 20 }
validates :type, presence: true
pharmacy.rb
class Pharmacy < Hospital
self.table_name = 'pharmacies'
end
这两个表拥有完全相同的列,我选择使用多表继承(MTI)来提高数据库的可扩展性,因为药房和医院也将使用单表继承(STI)来支持多个模型。
我遇到了以下错误:
ActiveRecord::SubclassNotFound:
Invalid single-table inheritance type: Hospital is not a subclass of Pharmacy
我想重用我打算为两个模型实现的验证和一些方法。
concern
文件夹里,对吧? - aplneto