Permalink
Browse files

Merge pull request #13107 from chancancode/has_one_counter_cache_shou…

…ld_raise

Raise `ArgumentError` when `has_one` is used with `counter_cache`
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/builder/belongs_to.rb
	activerecord/lib/active_record/associations/builder/singular_association.rb
  • Loading branch information...
1 parent ad29f6c commit 997f0787111a9687560be2321f6e804644888b0f @senny senny committed Nov 30, 2013
@@ -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*
+
* Update counter cache on a has_many relationship regardless of default scope
Fix #12952.
@@ -5,7 +5,7 @@ def macro
end
def valid_options
- super + [:foreign_type, :polymorphic, :touch]
+ super + [:foreign_type, :polymorphic, :touch, :counter_cache]
end
def constructable?
@@ -1,7 +1,7 @@
module ActiveRecord::Associations::Builder
class SingularAssociation < Association #:nodoc:
def valid_options
- super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of]
+ super + [:remote, :dependent, :primary_key, :inverse_of]
end
def constructable?
@@ -565,4 +565,12 @@ def test_has_one_autosave_with_primary_key_manually_set
assert_not_nil author.post
assert_equal author.post, post
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
@@ -314,4 +314,12 @@ def test_assigning_has_one_through_belongs_to_with_new_record_owner
def test_has_one_through_with_custom_select_on_join_model_default_scope
assert_equal clubs(:boring_club), members(:groucho).selected_club
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

0 comments on commit 997f078

Please sign in to comment.