Rails db:seed error "undefined method `finder_needs_type_condition?' for nil:NilClass"
Asked Answered
M

3

6

I have a problem when attempting to populate my sqlite db. There's not much info regarding the specific error "finder_needs_type_condition?" that I can find, but I don't have much experience with Rails yet to even suspect where the problem could be.

Model:

class Character < ActiveRecord::Base
  validates :user_id, :name, :class, presence: true
end

Controller:

class CharactersController < ApplicationController
  before_action :authenticate_user!
  respond_to :json

  @user_id = current_user[:id]

  def index
    @characters = Character.all
  end

  def show
    @character = Character.find(params[:id])
  end

  def new
    @character = Character.new
  end

  def create
    @characters = Character.all
    @character = Character.create(character_params)
  end

  private
    def character_params
      params.require(:character).permit(:user_id, :name, :class)
    end
end

schema.rb:

create_table "characters", force: :cascade do |t|
  t.datetime "created_at",             null: false
  t.datetime "updated_at",             null: false
  t.string   "name"
  t.string   "class"
  t.integer  "user_id",    default: 0, null: false
end

seeds.rb:

Character.delete_all
Character.create!([
  {id: 1, name: "nameone", class: "Warrior", user_id: 1},
  {id: 2, name: "nametwo", class: "Mage", user_id: 1},
  {id: 3, name: "namethree", class: "Wizard", user_id: 1},
  {id: 4, name: "namefour", class: "Warlock", user_id: 1},
  {id: 5, name: "namefive", class: "Rogue", user_id: 1}
])

And the error when attempting seed:

$ bundle exec rake db:seed
rake aborted!
NoMethodError: undefined method `finder_needs_type_condition?' for nil:NilClass
/var/rubyapps/test/db/seeds.rb:2:in `<top (required)>'
Tasks: TOP => db:seed

Have I misunderstood something when generating the model?

Meacham answered 24/9, 2015 at 12:39 Comment(1)
It's scary how similar this is to my problem, here's one of my test cases that are giving me errors :D let(:character1) { Character.create(user: user, name: "characterName", class: "Wizard") }}Neils
H
15

You cannot have a column name of class, because this is a reserved word. You're already using character_class in your seeds, which doesn't exist in the the database, so all you need to do is rename this column the database.

rails g migration RenameCharactersClassColumn

Then add this line to the migration file.

rename_column :characters, :class, :character_class

Run rake db:migrate and that should fix it.

Haggle answered 24/9, 2015 at 12:55 Comment(0)
A
1

Had the same error when I created a new rails database and tried to create a new entry via the front-end.

I had a column named 'class'. As it was a new database, it had no information so I simply deleted it and recreated it, but calling the column 'class' something different. Works fine, no more error message.

Anyplace answered 29/12, 2015 at 3:8 Comment(0)
Q
0

Replace character_class with class in your seeds.rb as you have column name class in characters table.

{id: 1, name: "nameone", class: "Warrior", user_id: 1},
Quadrillion answered 24/9, 2015 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.