用户#show中的ActiveRecord :: SubclassNotFound - 在rails中调试?

时间:2015-08-07 23:09:15

标签: ruby-on-rails debugging ruby-on-rails-4 inheritance activerecord

我有单表继承设置如下:

class Post < ActiveRecord::Base
 def self.types
    %w(Status Image)
  end
end

class Image < Post
///
end

class Status < Post
///
end

帖子的类型为图片或类型状态

这是我的(简化的)用户控制器 - 显示操作和相应的视图:

 def show
   @user = User.find(params[:id])
   @posts = @user.posts.order(created_at: :desc)
 end


<%@posts.each do |x|%>    (<- error : line 31) 
<%=x.body%>
<%end%>

以下是我遇到的错误:

Showing /app/views/users/show.html.erb where line #31 raised:


The single-table inheritance mechanism failed to locate the subclass: 'status'. 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 Post.inheritance_column to use another column for that information.

注意:我知道这个错误正在发生,因为只有这个特定用户的一些糟糕的帖子输入,我只是不知道如何调试这个 - 并找出确切的哪个帖子条目导致我的错误。我也知道如果我重置我的数据库,错误应该消失,但我真的想以正确的方式解决这个问题。 (如果我转到其他用户显示页面,则不会发生此错误,因此我知道这是一个连接到此用户的帖子)。

请帮忙吗?

注意:我尝试过以下内容(但仍然会遇到同样的错误):

  <%#=debug @posts%>
  <%#=@posts.inspect%>
  <%#=simple_format @posts.to_yaml%>

我知道我在查看这个问题的方式可能有一个缺陷,但我真的很感激向正确的方向迈进。

要重新迭代,我正在寻找一种调试此类问题的好方法(我过去遇到过这个问题),其中输入的数据导致代码中断

1 个答案:

答案 0 :(得分:0)

我的猜测是,您已将type的{​​{1}}字段存储为Post而不是status。注意资本S.如果没有看到数据本身就很难说。