正在使用错误ActiveModel :: UnknownAttributeError停止Rails种子迁移

时间:2017-05-03 20:06:58

标签: ruby-on-rails ruby ruby-on-rails-5

我一直在努力做rake db:seed这通常对我有用,但现在却失败了。

这是错误和代码。

我的错误日志: enter image description here

User.rb:

class User < ApplicationRecord  
cattr_accessor :current_user        
belongs_to :highschool, optional: true
end

高中移民:

class CreateHighschools < ActiveRecord::Migration[5.0]
def change
create_table :highschools do |t|
  t.string :secondaryschool

  t.timestamps
end
end
end

高中迁移参考用户:

class AddHighschoolRefToUsers < ActiveRecord::Migration[5.0]
def change
add_reference :users, :highschools, foreign_key: true
end
end

Highschool.rb:

class Highschool < ApplicationRecord
has_many :users
end

Highschools_controller.rb:

class HighschoolsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
def create
   @highschool = Highschool.new(highschool_params)
   if @highschool.save
  render json: @highschool
else
  render json: {errors: @highschool.errors.full_messages}
end
end
private
  def highschool_params
params.require(:highschool).permit(:secondaryschool)
end
end

Schema.rb:

 create_table "highschools", force: :cascade do |t|
t.string   "secondaryschool"
t.datetime "created_at",      null: false
t.datetime "updated_at",      null: false
  end

Seeds.rb:

Highschool.destroy_all
special = Highschool.create!(Secondaryschool: "Stuyvesant High School")
special2 = Highschool.create!(Secondaryschool: "Brooklyn Tech")
special3 = Highschool.create!(Secondaryschool: "Bronx Science")

1 个答案:

答案 0 :(得分:2)

Theres a typo:

special = Highschool.create!(secondaryschool: "Stuyvesant High School")
special2 = Highschool.create!(secondaryschool: "Brooklyn Tech")
special3 = Highschool.create!(secondaryschool: "Bronx Science")