From 5b2332ac71e39fbdba0067cdf6231a8445f5e29e Mon Sep 17 00:00:00 2001 From: glaszig Date: Fri, 18 Dec 2020 04:38:36 +0100 Subject: [PATCH] allow passing false to :polymorphic option of belongs_to before this, passing false would raise the following error because a condition in AR would disregard the option entirely if false was passed. ArgumentError: Unknown key: :polymorphic. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :counter_cache, :optional, :default --- activerecord/CHANGELOG.md | 7 +++++++ .../lib/active_record/associations/builder/belongs_to.rb | 4 ++-- .../cases/associations/belongs_to_associations_test.rb | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fe6414ce47..7f9d3766c3 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,10 @@ +* Restore possibility of passing `false` to :polymorphic option of `belongs_to`. + + Previously, passing `false` would trigger the option validation logic + to throw an error saying :polymorphic would not be a valid option. + + *glaszig* + * Remove deprecated `database` kwarg from `connected_to`. *Eileen M. Uchitelle*, *John Crepezzi* diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 869b228dc7..584af2c3f2 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -7,8 +7,8 @@ module ActiveRecord::Associations::Builder # :nodoc: end def self.valid_options(options) - valid = super + [:counter_cache, :optional, :default] - valid += [:polymorphic, :foreign_type] if options[:polymorphic] + valid = super + [:polymorphic, :counter_cache, :optional, :default] + valid += [:foreign_type] if options[:polymorphic] valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async valid end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index ab7b60e3af..db5063c197 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -1354,6 +1354,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal toy, sponsor.reload.sponsorable end + def test_polymorphic_with_false + assert_nothing_raised do + Class.new(ActiveRecord::Base) do + def self.name; "Post"; end + belongs_to :category, polymorphic: false + end + end + end + test "stale tracking doesn't care about the type" do apple = Firm.create("name" => "Apple") citibank = Account.create("credit_limit" => 10)