Skip to content

Commit 41e3121

Browse files
authored
Merge pull request rails#51408 from viralpraxis/add-through-to-has-many-and-has-one-association-options-list
Ensure all needed options are added to association options list unconditionally
2 parents 7242fd7 + 3bd2d68 commit 41e3121

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ def self.macro
77
end
88

99
def self.valid_options(options)
10-
valid = super + [:counter_cache, :join_table, :index_errors]
11-
valid += [:as, :foreign_type] if options[:as]
12-
valid += [:through, :source, :source_type] if options[:through]
10+
valid = super + [:counter_cache, :join_table, :index_errors, :as, :through]
11+
valid += [:foreign_type] if options[:as]
12+
valid += [:source, :source_type, :disable_joins] if options[:through]
1313
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
14-
valid += [:disable_joins] if options[:disable_joins] && options[:through]
1514
valid
1615
end
1716

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ def self.macro
77
end
88

99
def self.valid_options(options)
10-
valid = super
11-
valid += [:as, :foreign_type] if options[:as]
10+
valid = super + [:as, :through]
11+
valid += [:foreign_type] if options[:as]
1212
valid += [:ensuring_owner_was] if options[:dependent] == :destroy_async
13-
valid += [:through, :source, :source_type] if options[:through]
14-
valid += [:disable_joins] if options[:disable_joins] && options[:through]
13+
valid += [:source, :source_type, :disable_joins] if options[:through]
1514
valid
1615
end
1716

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,22 @@ def test_key_ensuring_owner_was_is_not_valid_without_dependent_option
31843184
assert_match(/Unknown key: :ensuring_owner_was/, error.message)
31853185
end
31863186

3187+
def test_invalid_key_raises_with_message_including_all_default_options
3188+
error = assert_raises(ArgumentError) do
3189+
Class.new(ActiveRecord::Base) do
3190+
has_many :books, trough: :users
3191+
end
3192+
end
3193+
3194+
assert_equal(<<~MESSAGE.squish, error.message)
3195+
Unknown key: :trough. Valid keys are:
3196+
:class_name, :anonymous_class, :primary_key, :foreign_key, :dependent,
3197+
:validate, :inverse_of, :strict_loading, :query_constraints, :autosave, :before_add,
3198+
:after_add, :before_remove, :after_remove, :extend, :counter_cache, :join_table,
3199+
:index_errors, :as, :through
3200+
MESSAGE
3201+
end
3202+
31873203
def test_key_ensuring_owner_was_is_valid_when_dependent_option_is_destroy_async
31883204
Class.new(ActiveRecord::Base) do
31893205
self.destroy_association_async_job = Class.new

0 commit comments

Comments
 (0)