Marty Marty - 4 years ago 164
Ruby Question

Seeding fails upon `enum`

I have problems using

enum
. I looked at the documentation but don't understand what I'm doing wrong (sorry, I'm a newbie). I my model file I have:

enum origin: [ :website, :stand, :other ]


In my seeds file I create a new record with one of the key-value pairs being
origin: "website"
:

content = Faker::Lorem.paragraphs(2)
author.articles.create!( title: "Title",
origin: "website",
content: content )


Upon seeding this generates the error:

ActiveRecord::SubclassNotFound: Invalid single-table inheritance type: website is not a subclass of Article


What could be the cause of this error?

Update: I'm reading the same issue here: https://github.com/rails/rails/issues/14136. But I don't understand it. Could someone please explain?

Article.rb:

class Article < ActiveRecord::Base
belongs_to :author
has_many :links
validates :author_id, presence: true
end

Answer Source

This should work:

author.articles.create!( title: "Title",
                         origin: :website,
                         content: content )

source for enums: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

for an in-depth read about enums: https://hackhands.com/ruby-on-enums-queries-and-rails-4-1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download