asalgan
asalgan

Reputation: 336

Rails single table inheritance ActiveRecord::SubclassNotFound

I tried changing a User type to "nil" and keep getting this error message now:

The single-table inheritance mechanism failed to locate the subclass: 'nil'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite User.inheritance_column to use another column for that information.

from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/inheritance.rb:161:in `rescue in find_sti_class'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/inheritance.rb:155:in `find_sti_class'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/inheritance.rb:144:in `discriminate_class_for_record'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/persistence.rb:50:in `instantiate'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `block in find_by_sql'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `block in each'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `each'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/result.rb:55:in `each'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `map'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/querying.rb:48:in `find_by_sql'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/relation.rb:603:in `exec_queries'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/relation.rb:487:in `load'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/relation.rb:231:in `to_a'
from /usr/local/rvm/gems/[email protected]/gems/bullet-4.13.1/lib/bullet/active_record41.rb:10:in `to_a'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/relation/finder_methods.rb:479:in `find_last'
from /usr/local/rvm/gems/[email protected]/gems/activerecord-4.1.1/lib/active_record/relation/finder_methods.rb:165:in `last'

I understand why it's happening and I do want the single table inheritance to occur but it won't even let me override it Rails Console. I'm trying to change the User type back to what I need and it immediately gives me that message. Even if I do User.find(myID) it won't pull up my data.

Any way I can just tell it to override it and change the type?

Edit:

I tried:

User.last (I was the last user)

User.find(*myId*)

It won't even let me do User.last.destroy or User.destroy_all, giving me the same error above each time in the console.

I also tried creating a model called "nil" that inherits from User and still nothing.

Upvotes: 0

Views: 3957

Answers (1)

D-side
D-side

Reputation: 9495

Well, you messed up a little :)

Rails' Single Table Inheritance expects the exact class name in type field. Since "nil" does not specify a valid User subclass, you have created objects that make no sense in terms of STI.

It's easy to fix. Select all the errorneous entries (containing "nil" in type) and fix them (set type to nil, that will become SQL NULL that means "base class" here):

User.where(type: "nil").update_all(type: nil)

Upvotes: 7

Related Questions