I have a few tables in my application that share most columns. I wrote all validations in one model and tried to make all other models inherit from this one but got an ActiveRecord::SublassNotFound
error.
Here is the code for my models:
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
Both tables have the exact same columns and I choose to use MTI to give my database more scalability, since both pharmacy and hospital will also have STI to multiple models.
This is the error I'm getting:
ActiveRecord::SubclassNotFound:
Invalid single-table inheritance type: Hospital is not a subclass of Pharmacy
I would like to reuse the validations and some methods I intend to implement for both models.