diff options
author | Prathamesh Sonpatki <csonpatki@gmail.com> | 2019-04-16 11:42:18 +0530 |
---|---|---|
committer | Prathamesh Sonpatki <csonpatki@gmail.com> | 2019-04-19 20:08:29 +0530 |
commit | 8b4d344815655027d9f7584c0a59271dce8f1d5a (patch) | |
tree | cf98e4e0c22a1584fb574d10ef18cae56374eb36 /railties/lib/rails | |
parent | 10da0a27512e108a5cde3eeba774b01c15f6c43a (diff) | |
download | rails-8b4d344815655027d9f7584c0a59271dce8f1d5a.tar.gz rails-8b4d344815655027d9f7584c0a59271dce8f1d5a.tar.bz2 rails-8b4d344815655027d9f7584c0a59271dce8f1d5a.zip |
Add `null: false` constraint by default for `belongs_to` associations
- Also deprecate passing {required} to the model generator.
- Also made sure the global config `belongs_to_required_by_default` is
applied correctly to the model generator for `null: false` option.
Diffstat (limited to 'railties/lib/rails')
-rw-r--r-- | railties/lib/rails/generators/generated_attribute.rb | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index 99c1bc4269..1a80e71eae 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/time" +require "active_support/deprecation" module Rails module Generators @@ -51,6 +52,12 @@ module Rails type = $1 provided_options = $2.split(/[,.-]/) options = Hash[provided_options.map { |opt| [opt.to_sym, true] }] + + if options[:required] + ActiveSupport::Deprecation.warn("Passing {required} option has no effect on the model generator. It will be removed in Rails 6.1.\n") + options.delete(:required) + end + return type, options else return type, {} @@ -137,7 +144,7 @@ module Rails end def required? - attr_options[:required] + reference? && Rails.application.config.active_record.belongs_to_required_by_default end def has_index? @@ -183,7 +190,6 @@ module Rails def options_for_migration @attr_options.dup.tap do |options| if required? - options.delete(:required) options[:null] = false end |