我一直试图做rake db:seed
这通常适用于我,但现在失败。Rails种子迁移正在停止,并出现错误ActiveModel :: UnknownAttributeError
这是错误和代码。
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")
请勿将图像用于错误,数据或代码等信息。链接可能腐烂和破坏,删除对问题至关重要的信息。另外,我们不能重复使用这些信息。通常我们搜索错误的部分内容,这迫使我们输入它而不是复制/粘贴。此外,搜索引擎无法为图片编制索引,因此在寻找相同问题的帮助时,其他人难以找到您的问题。请记住,SO并没有像回答帮助你那么多,而是因为它是在帮助未来的同一个问题。 –
伟大的观点,我一定会这样做,即前进@theTinMan – Omar