3

I'm getting this error and I don't know what to do. It says

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

It says that this error is occuring at the 9 in games_controller.rb.

i.e.

   @game = current_user.created_games.build(game_params)

I don't understand what is happening and what I should write here to specify the cause of this error. I copy&paste game.rb, user.rb, application_controller.rb and games_controller.rb here.

game.rb is

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

user.rb is

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 is

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

Added

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 %>

When I try to create new game, console says as follows.

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:
  • 2
    I've seen this error before. Something in your params is messed up. Can you post the view containing the Game form? – Ryan K Jan 28 '15 at 16:16
  • also can you post the request you get in console when you submit new game? – Miknash Jan 28 '15 at 16:25
  • Please edit your post with the new code. And, so you don't have a form for the game? How are you submitting the create request? – Ryan K Jan 28 '15 at 16:31
  • Can I start a new line in one comment? –  Jan 28 '15 at 16:33
  • Yes, you can use SHIFT-ENTER to get a new line. However, please edit your post (at the bottom) if you have a lot of additional information to share. – Ryan K Jan 28 '15 at 16:34
  • I misunderstood. The form is exist. I will copy. –  Jan 28 '15 at 16:34
  • 1
    See http://stackoverflow.com/questions/7134559/rails-use-type-column-without-sti You can't use `type` as a column name. – ptd Jan 28 '15 at 16:38
  • I remember now. Yeah, you have `type` as a column name in your migration. I've seen ways to keep it that name but it's just easier to change the column name. – Ryan K Jan 28 '15 at 16:40

1 Answers1

5

So to answer the question, you cannot have a column name called type in your migrations. I believe there are ways around this, but it's good practice to just rename your column something else.

Ryan K
  • 3,985
  • 4
  • 39
  • 42