ruby-on-railsruby-on-rails-4enumsactivemodel

In Rails 4, how do I reference an enum value when initializing my model?


I’m using Rails 4.2. It is not an option to upgrade at this time. I have this model

class OrderItem < ApplicationRecord
    …
  enum type: { data: “Data”, product: “Product” }

How do I initialize my object by referencing my enum? I tried this

@order_item = OrderItem.new(
  order_item_params.merge(
    type: :product
  )
)
…
@order_item.save

But this results in the error

 NoMethodError:
   undefined method `safe_constantize' for :product:Symbol

Solution

  • TLDR:

    class OrderItem < ApplicationRecord
      self.inheritance_column = 'definitely_not_type'
      enum type: { data: "Data", product: "Product" }
    

    type is the default for the inheritance_column in Rails. This column is mainly used for Single Table Inheritance. When it is present ActiveRecord will use the values in this column as the class for each row it fetches from the database.

    So given the table animals with these values:

    id | type   | name
    ------------------------
    1  | Cat    | Mr Mittens
    2  | Dog    | Laika
    3  | Snake  | Snek
    

    When you call Animal.all.map {|m| m.class} you will get [Cat, Dog, Snake]. Or at least you will if those contants actually exist.

    If you're not actually using STI and want to use the name type you can just assign whatever you want to self.inheritance_column.