Models `belongs_to` association required by default.

In Ruby on Rails 5 belongs_to associations required by default so the following code:

class User < ApplicationRecord

class Post < ApplicationRecord
  belongs_to :user # to make it work in Rails 4.x you need to add `required: true`

post = Post.create(title: 'Hi')
#=> <Post id: nil, title: "Hi", user_id: nil, created_at: nil, updated_at: nil>


will throw an error.

#=> "User must exist"

It could be enabled or disabled in application configuration.

Rails.application.config.active_record.belongs_to_required_by_default = false|true
Alexander M about 6 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more