LoginSignup
0
0

More than 1 year has passed since last update.

Validation failed: # must exist 解決法 

Posted at

はじめに

Railsでポートフォリオを開発中に、rails consoleで投稿機能がしっかりデータベースに入っているかを確認した。
しかし、model.saveで実行した結果・・・
=>false
という結果が出た。

データベースに保存できていない。

試したこと

model.save!
上記の様に!をつけてエラーがどこにあるのかを確認できる。

そうすると・・・
ActiveRecord::RecordInvalid (Validation failed: モデル must exist)

というエラーが出てきた。

モデル.rb
class モデル < ApplicationRecord
 belongs_to :user, optional: true
end

optional: true
をつけるだけで問題が解決した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0