私はlogIn用のdevise gemを私のアプリに追加しました。マイページでは、お店で、一人のユーザーは、多くの注文を持っていると私は、モデルのユーザーに追加します。Ruby on Rails - rake db:seed aborted
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
そして今、私は」:私はこれを追加
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(受注)の移行ファイルにシードファイルでDBをテストしていますが、ユーザにComandaを追加すると、レーキが打ち切られるというエラーが発生します。
マイシーズファイル:
c1 = Comanda.create(total: 100)
u1 = User.find_by email: "[email protected]"
u1.comandas << [c1]
は前にユーザーをI'created。
エラー: Screen shot of shell
UPDATE:
は私が最終的に問題を発見しました。問題は、この変更を加える前にすべてのモデルを作成したことでした。そして、この関係を追加すると、Railsはデータベースを変更しませんでした。私はこれをsqliteコマンドラインに入れ、Comandasテーブルの列を列挙してチェックしました。私は、ユーザーへの言及がここにないことを知りました。
rake db:drop db:create db:migrate
そして、私は、ユーザーを追加し、私が実行した後:
rake db:seed
と連携し、このコマンドを実行するすべてのデータベースをドロップすることを決定! 助けてくれてありがとう!
まずユーザーを作成しませんか? – ediblecode
rake db:seedを最後の行なしで実行すると、私はすでにユーザーを作成しています。ここでComandaをUserに追加すると動作します。 – Xavi
最後の行を削除しても、 'u1'がヌルではないことが証明されていません。 – ediblecode