我一直在努力做rake db:seed
这通常对我有用,但现在却失败了。
这是错误和代码。
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")
答案 0 :(得分:2)
Theres a typo:
special = Highschool.create!(secondaryschool: "Stuyvesant High School")
special2 = Highschool.create!(secondaryschool: "Brooklyn Tech")
special3 = Highschool.create!(secondaryschool: "Bronx Science")