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

Raise ArgumentError when has_one is used with counter_cache

Previously, the `has_one` macro incorrectly accepts the `counter_cache` option
due to a bug, although that options was never supported nor functional on
`has_one` and `has_one ... through` relationships. It now correctly raises an
`ArgumentError` when passed that option.

For reference, this bug was introduced in 52f8e4b9.
This commit is contained in:
Godfrey Chan 2013-11-29 19:21:49 -08:00
parent 31c20a244b
commit 35fd2d4019
5 changed files with 25 additions and 2 deletions

View file

@ -1,3 +1,10 @@
* Previously, the `has_one` macro incorrectly accepts the `counter_cache`
option due to a bug, although that options was never supported nor
functional on `has_one` and `has_one ... through` relationships. It now
correctly raises an `ArgumentError` when passed that option.
*Godfrey Chan*
* Implement rename_index natively for MySQL >= 5.7. * Implement rename_index natively for MySQL >= 5.7.
*Cody Cutrer* *Cody Cutrer*

View file

@ -5,7 +5,7 @@ module ActiveRecord::Associations::Builder
end end
def self.valid_options(options) def self.valid_options(options)
super + [:foreign_type, :polymorphic, :touch] super + [:foreign_type, :polymorphic, :touch, :counter_cache]
end end
def self.valid_dependent_options def self.valid_dependent_options

View file

@ -3,7 +3,7 @@
module ActiveRecord::Associations::Builder module ActiveRecord::Associations::Builder
class SingularAssociation < Association #:nodoc: class SingularAssociation < Association #:nodoc:
def self.valid_options(options) def self.valid_options(options)
super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of] super + [:remote, :dependent, :primary_key, :inverse_of]
end end
def self.define_accessors(model, reflection) def self.define_accessors(model, reflection)

View file

@ -549,4 +549,12 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_not_nil author.post assert_not_nil author.post
assert_equal author.post, post assert_equal author.post, post
end end
def test_has_one_relationship_cannot_have_a_counter_cache
assert_raise(ArgumentError) do
Class.new(ActiveRecord::Base) do
has_one :thing, counter_cache: true
end
end
end
end end

View file

@ -315,4 +315,12 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
def test_has_one_through_with_custom_select_on_join_model_default_scope def test_has_one_through_with_custom_select_on_join_model_default_scope
assert_equal clubs(:boring_club), members(:groucho).selected_club assert_equal clubs(:boring_club), members(:groucho).selected_club
end end
def test_has_one_through_relationship_cannot_have_a_counter_cache
assert_raise(ArgumentError) do
Class.new(ActiveRecord::Base) do
has_one :thing, through: :other_thing, counter_cache: true
end
end
end
end end