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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With