rubyormmodelsequeldefault-scope

Single Table Inheritance in Sequel::Model (Ruby ORM)


I have a table in my database called providers with a type column called provider_type. provider_type can be either of the following:

I want to create a class that inherits from Sequel::Model called Center and one called Sponsor, the resulting methods for which will scope all queries for the respective class by provider_type = 'center' or provider_type = 'sponsor'.

It's not 100% essential that I be able to do this but if it is possible, it would be ideal.


Solution

  • You want the single_table_inheritance plugin:

    class Provider < Sequel::Model
      plugin :single_table_inheritance, :provider_type
    end
    class Center < Provider
    end
    class Sponsor < Provider
    end
    

    This will work, but only if the provider_type column matches exactly "Center" or "Sponsor". If not, you might need to add a :model_map option to the plugin call. The documentation on this plugin is located at http://sequel.jeremyevans.net/rdoc-plugins/classes/Sequel/Plugins/SingleTableInheritance.html