1

I have a Dog model that can have many tags and my Tag model has the column type. For the type attribute I can only choose 3 different types:

class Tag
 belongs_to :dog
 TYPES = %w(Red Blue Green)

 validates :type, inclusion: { :in => TYPES },
                  presence: true
end

In my seed file I have:

tags = Tag.create( [ { name: "Red Terrier", type: "Red" } ] )

I run the rake command and get the error:

rake aborted!
Invalid single-table inheritance type: Red is not a subclass of Tag

What's going on here? What's the way to do this and why does it think it's a subclass?

user2784630
  • 796
  • 1
  • 8
  • 19

3 Answers3

2

Do not use ‘type‘ as a column name. It's reserved in Rails. Rename it and it will work

gotva
  • 5,919
  • 2
  • 25
  • 35
1

type is a reserved column name that rails uses for polymorphic models. Try renaming your type column to dog_type and it should work

See: http://guides.rubyonrails.org/active_record_basics.html#schema-conventions

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
1

You ran in to a bug because of what's going on behind the scenes in Rails. Basically type is a field that is used for ActiveRecord inheritance, so having a field also named type is messing things up

One possible solution: create a new migration, and just use rename_column to give the type column a different name.

Another possible solution is given here: Rails: Invalid single-table inheritance type error, although I don't personally know the implications of doing this.

Community
  • 1
  • 1
JKillian
  • 18,061
  • 8
  • 41
  • 74