I was hoping I could inherit off an activerecord model, and use the subclass as I could the parent class. This does not appear to be the case, the AR relationships do not appear to work for the subclass.
class Manager < User
belongs_to :shop
end
class Shop < ActiveRecord::Base
has_many :managers
end
class PremiumShop < Shop
end
and
@premium_shop = manager.shop # Finds the shop.
@premium_shop = manager.premium_shop # Does not find the shop, NilClass:Class error
Is it possible to make this work?
The
shopmethod exists for some instance of theManagerclass because of the association you defined through thebelongs_to. You have nopremium_shopmethod defined on yourManagermodel, thus theNilClasserror.If you want to define such an association for the
PremiumShopclass, you must explicitly specify this.Depending on your needs, you might also consider researching “rails single table inheritance”.