1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00
Files
til/rails/allow-associations-to-be-optional.md

1.1 KiB
Raw Blame History

Allow Associations To Be Optional

As of Rails 5, whenever a belongs_to association is declared in an ActiveRecord model, it is assumed to be a required association.

class Book < ApplicationRecord
  belongs_to :author
end

If we were to create a Book instance without an Author, then we would get an error Validation Failed: Author is missing.

We could either make sure to always create books with authors or, if it makes sense for our data model, we could treat the author as optional.

The most explicit and precise way to make a relation optional is to declare it as such in the belongs_to directive.

class Book < ApplicationRecord
  belongs_to :author, optional: true
end

Another approach, though I don't recommend it unless you have a strong reason, is to globally make associations optional. You can do this by adding the following line to your config/application.rb file.

config.active_record.belongs_to_required_by_default = false

source