我在我的應用程序中添加了logIn的devise gem。我的頁面是一家商店和一個用戶有很多訂單,我在模型用戶添加:Ruby on Rails - rake db:seed aborted
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :comandas #I add this line, comandas = orders in Spanish
end
而且在Comandas(訂單)的遷移文件添加此:
class CreateComandas < ActiveRecord::Migration[5.0]
def change
create_table :comandas do |t|
t.belongs_to :user, index: true #references to user
t.float :total
t.timestamps
end
end
end
而且現在我m用種子文件測試數據庫,但是當我向用戶添加一個Comanda時,這引起了rake中止的錯誤。前
c1 = Comanda.create(total: 100)
u1 = User.find_by email: "[email protected]"
u1.comandas << [c1]
I'created用戶:
我的種子文件。
UPDATE:
我終於發現了問題。問題在於我在添加此修改之前創建了所有模型。當我添加這個關係時,Rails並沒有改變我的數據庫。我檢查了這個進入sqlite命令行並列出了Comandas表的列。我看到用戶的參考不在這裏。並決定放棄所有數據庫中執行以下命令:
rake db:drop db:create db:migrate
後,我增加了一個用戶,我跑:
rake db:seed
和作品! 感謝大家的幫助!
你不想先創建用戶嗎? – ediblecode
我已經創建了用戶,因爲當我執行rake db:種子沒有最後一行時,我將Comanda添加到用戶,它工作。 – Xavi
刪除最後一行並不證明'u1'不爲空 – ediblecode