0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

xxx must be exist

0
Posted at

タイトルのエラーにかなりハマったのでメモを残します。

環境

rails 6.1.7
ruby 3.0.6

アソシエーション設定

bookとcategoryが1:nの関係とします。
bookが複数のcategoryを持つのでcategoryテーブルにbook_idという外部キーを設定します。
この時book_idにnot null制約をつけない場合はすこし注意が必要です。

belongs_toの方にoptional: trueを設定する必要があります。

class Book < ApplicationRecord
  has_many :categories
end
class Category < ApplicationRecord
  belongs_to :book, optional: true
end
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?