我一直試圖做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