belongs_to の optional: true
belongs_to の外部キーの nil を許可するオプション。
code:ruby
class User < ApplicationRecord
has_many :events
end
code:ruby
class Event < ApplicationRecord
belongs_to :user, optional: true
end
Event モデルは外部キーとして user_id を持つことになるが、 user_id の nil を許容することができる。
とはいえ DB の null も許容する必要があるので注意が必要。