无效的单表继承类型:

时间:2015-01-28 16:13:39

标签: ruby-on-rails

我收到此错误,我不知道该怎么做。它说

Invalid single-table inheritance type: 1 is not a subclass of Game

它表示这个错误发生在games_controller.rb中的9处。

   @game = current_user.created_games.build(game_params)

我不明白发生了什么以及我应该写什么来指明这个错误的原因。我在这里复制并粘贴 game.rb user.rb application_controller.rb games_controller.rb 。< / p>

game.rb

class Game < ActiveRecord::Base
  belongs_to :owner, class_name: 'User'
end

user.rb

class User < ActiveRecord::Base
  has_many :created_games, class_name: 'Game', foreign_key: :owner_id

  def self.find_or_create_from_auth_hash(auth_hash)
    provider = auth_hash[:provider]
    uid = auth_hash[:uid]
    nickname = auth_hash[:info][:nickname]
    image_url = auth_hash[:info][:image]

    User.find_or_create_by(provider: provider, uid: uid) do |user|
      user.nickname = nickname
      user.image_url = image_url
    end
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  helper_method :logged_in?

  private

  def current_user
    return unless session[:user_id]
    @current_user ||= User.find(session[:user_id])
  end

  def logged_in?
    !!session[:user_id]
  end

  def authenticate
    return if logged_in?
    redirect_to root_path, alert:'Please log in.'
  end

  def created_by?(user)
    return false unless user
    owner_id == user.id
  end
end

games_controller.rb

class GamesController < ApplicationController
  before_action :authenticate

  def new
    @game = current_user.created_games.build
  end

  def create
    @game = current_user.created_games.build(game_params)
    if @game.save
      redirect_to @game, notice: 'You created game.'
    else
      render :new
    end
  end

  private

  def game_params
    params.require(:game).permit(
      :name, :content, :type
    )
  end
end

new.html.erb

<% now = Time.zone.now %>
<div class="page-header">
  <h1>Create Game</h1>
</div>

<%= form_for(@game, class: 'form-horizontal', role:'form') do |f| %>
  <% if @game.errors.any? %>
    <div class="alert alert-danger">
      <ul>
      <% @game.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :type %>
    <div>
      <%= f.select :type, options_for_select: [['basic', 1]], class: 'form-control' %>
    </div>
  </div>
  <div class="form-group">
    <%= f.label :content %>
    <%= f.text_area :content, class: 'form-control', row: 10 %>
  </div>
  <%= f.submit 'Create', class: 'btn btn-default', data: { disable_with: 'Creating...' } %>
<% end %>

当我尝试创建新游戏时,控制台说如下。

Started POST "/games" for 127.0.0.1 at 2015-01-29 01:50:29 +0900
Processing by GamesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2W57cDKT2552Xgnh2MLi17uQpcrqDkmxhQJQa1qNuJNsZb0R10l/AU37y+KO9DysCp56aVTFBE/MqRoimMnjYQ==", "game"=>{"name"=>"this is name", "type"=>"1", "content"=>"this is content"}, "commit"=>"Create"}
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 32ms

ActiveRecord::SubclassNotFound - Invalid single-table inheritance type: 1 is not a subclass of Game:

1 个答案:

答案 0 :(得分:5)

因此,要回答此问题,您的迁移中不能有名为type的列名。我相信有很多方法可以解决这个问题,但是将列重命名为其他内容是一种很好的做法。