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?
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.
You cannot have a column name of
class
, because this is a reserved word. You're already usingcharacter_class
in your seeds, which doesn't exist in the the database, so all you need to do is rename this column the database.Then add this line to the migration file.
Run
rake db:migrate
and that should fix it.Replace
character_class
withclass
in your seeds.rb as you have column nameclass
in characters table.