ruby-on-railsrubyactiverecordenumssingle-table-inheritance

What are the cons of using enum field for single table inheritance in rails?


I am refactoring an existing model using single table inheritance because the model logic has too many paths based on a column which is enum.

I want to pull out one type at a time keeping the other things as it is.

I used the existing column instead of adding new type column example code

class Experiment < ApplicationRecord
  self.inheritance_column = :experiment_type 

  enum experiment_type: {
    CampaignExperiment: 0,
    UserExperiment: 1,
  }
end

class CampaignExperiment < Experiment
end


This code works now and specs are passing. But rails docs suggests to use type column which is string. What are the things I should take care if I am using enum? Will it backfire in the future because its enum?


Solution

  • Using enum fails in runtime when Experiment controller tries to create an specific type of experiment by passing experiment_type attribute.

    Since I want to migrate one experiment_type at a time still keeping how experiment_type was set via controller when a request is processed.

    I had to add a new column type and keep that in sync with experiment_type so I could pull out on experiment type to its own subclass without breaking anything else.