Rails optional belongs_to

Just an update for rails 5, if you want this kind of behavior you will need to pass this option:

belongs_to :user, optional: true

In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default.

Update
If you still want to use the old behavior by default you can add the configuration to your application.rb file.

# /config/application.rb
config.active_record.belongs_to_required_by_default = false

notice: there were some issues on early releases of Rails 5 with this configuration, but is currently fixed, tested on the release candidate 5.2.3.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)