Rails单表继承ActiveRecord :: SubclassNotFound

时间:2015-02-06 16:19:09

标签: ruby-on-rails activerecord

我尝试将用户类型更改为" nil"并继续收到此错误消息:

  

单表继承机制无法找到子类:' nil'。提出此错误是因为列'键入'保留用于存储继承的类。如果您不打算将此列用于存储继承类或覆盖User.inheritance_column以使用另一列来获取该信息,请重命名此列。

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

我理解它为什么会发生,我确实希望单表继承发生,但它甚至不让我覆盖它Rails控制台。我试图将用户类型更改回我需要的内容,并立即向我提供该消息。即使我使用User.find( myID ),它也不会提取我的数据。

我可以告诉它覆盖它并更改类型吗?

编辑:

我试过了:

User.last (I was the last user)

User.find(*myId*)

它甚至不让我User.last.destroyUser.destroy_all,在控制台中每次都给出同样的错误。

我还尝试创建一个名为" nil"继承自用户,但仍然没有。

1 个答案:

答案 0 :(得分:3)

嗯,你搞砸了一点:)

滑轨'单表继承需要type字段中的确切类名。由于"nil"未指定有效的User子类,因此您创建的对象在STI方面毫无意义。

很容易解决。选择所有错误条目(包含"nil"中的type)并修复它们(将type设置为nil,这将成为SQL NULL,这意味着&#34 ;基类"这里):

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