1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
glaszig 2020-12-18 04:38:36 +01:00
parent c89632abf0
commit 5b2332ac71
3 changed files with 18 additions and 2 deletions

View file

@ -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*

View file

@ -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

View file

@ -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)