Rails db:seed error “undefined method `finder_needs_type_condition?' for nil:NilClass”

落花浮王杯 提交于 2019-12-11 04:26:55

问题


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?


回答1:


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.




回答2:


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},



回答3:


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.



来源:https://stackoverflow.com/questions/32761577/rails-dbseed-error-undefined-method-finder-needs-type-condition-for-nilnil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!