Skip to content

Commit de51649

Browse files
authored
Merge pull request rails#55089 from rails/fxn/class_name
:class_name should be invalid in polymorphic belongs_to
2 parents d1baada + a4a1996 commit de51649

File tree

7 files changed

+11
-10
lines changed

7 files changed

+11
-10
lines changed

activerecord/lib/active_record/associations/builder/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class << self
1919
self.extensions = []
2020

2121
VALID_OPTIONS = [
22-
:class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :query_constraints
22+
:anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :query_constraints
2323
].freeze # :nodoc:
2424

2525
def self.build(model, name, scope, options, &block)

activerecord/lib/active_record/associations/builder/belongs_to.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ def self.macro
88

99
def self.valid_options(options)
1010
valid = super + [:polymorphic, :counter_cache, :optional, :default]
11-
valid += [:foreign_type] if options[:polymorphic]
12-
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
11+
valid << :class_name unless options[:polymorphic]
12+
valid << :foreign_type if options[:polymorphic]
13+
valid << :ensuring_owner_was if options[:dependent] == :destroy_async
1314
valid
1415
end
1516

activerecord/lib/active_record/associations/builder/collection_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CollectionAssociation < Association # :nodoc:
77
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
88

99
def self.valid_options(options)
10-
super + [:before_add, :after_add, :before_remove, :after_remove, :extend]
10+
super + [:class_name, :before_add, :after_add, :before_remove, :after_remove, :extend]
1111
end
1212

1313
def self.define_callbacks(model, reflection)

activerecord/lib/active_record/associations/builder/has_one.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def self.macro
77
end
88

99
def self.valid_options(options)
10-
valid = super + [:as, :through]
10+
valid = super + [:class_name, :as, :through]
1111
valid += [:foreign_type] if options[:as]
1212
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
1313
valid += [:source, :source_type, :disable_joins] if options[:through]

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3198,8 +3198,8 @@ def test_invalid_key_raises_with_message_including_all_default_options
31983198

31993199
assert_equal(<<~MESSAGE.squish, error.message)
32003200
Unknown key: :trough. Valid keys are:
3201-
:class_name, :anonymous_class, :primary_key, :foreign_key, :dependent,
3202-
:validate, :inverse_of, :strict_loading, :query_constraints, :autosave, :before_add,
3201+
:anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of,
3202+
:strict_loading, :query_constraints, :autosave, :class_name, :before_add,
32033203
:after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table,
32043204
:index_errors, :as, :through
32053205
MESSAGE

activerecord/test/models/cpk/comment.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Cpk
44
class Comment < ActiveRecord::Base
55
self.table_name = :cpk_comments
6-
belongs_to :commentable, class_name: "Cpk::Post", foreign_key: %i[commentable_title commentable_author], polymorphic: true
7-
belongs_to :post, class_name: "Cpk::Post", foreign_key: %i[commentable_title commentable_author]
6+
belongs_to :commentable, foreign_key: %i[commentable_title commentable_author], polymorphic: true
7+
belongs_to :post, foreign_key: %i[commentable_title commentable_author]
88
end
99
end

activerecord/test/models/sharded/blog_post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class BlogPost < ActiveRecord::Base
55
self.table_name = :sharded_blog_posts
66
query_constraints :blog_id, :id
77

8-
belongs_to :parent, class_name: name, polymorphic: true
8+
belongs_to :parent, polymorphic: true
99
belongs_to :blog
1010
has_many :comments
1111
has_many :delete_comments, class_name: "Sharded::Comment", dependent: :delete_all

0 commit comments

Comments
 (0)