Skip to content
This repository
Browse code

Merge pull request #13107 from chancancode/has_one_counter_cache_shou…

…ld_raise

Raise `ArgumentError` when `has_one` is used with `counter_cache`
  • Loading branch information...
commit 61ad8d5c32422c523293428d25277be1759b597c 2 parents 31c20a2 + 35fd2d4
Yves Senn authored
7  activerecord/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,10 @@
  1
+*   Previously, the `has_one` macro incorrectly accepts the `counter_cache`
  2
+    option due to a bug, although that options was never supported nor
  3
+    functional on `has_one` and `has_one ... through` relationships. It now
  4
+    correctly raises an `ArgumentError` when passed that option.
  5
+
  6
+    *Godfrey Chan*
  7
+
1 8
 *   Implement rename_index natively for MySQL >= 5.7.
2 9
 
3 10
     *Cody Cutrer*
2  activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -5,7 +5,7 @@ def self.macro
5 5
     end
6 6
 
7 7
     def self.valid_options(options)
8  
-      super + [:foreign_type, :polymorphic, :touch]
  8
+      super + [:foreign_type, :polymorphic, :touch, :counter_cache]
9 9
     end
10 10
 
11 11
     def self.valid_dependent_options
2  activerecord/lib/active_record/associations/builder/singular_association.rb
@@ -3,7 +3,7 @@
3 3
 module ActiveRecord::Associations::Builder
4 4
   class SingularAssociation < Association #:nodoc:
5 5
     def self.valid_options(options)
6  
-      super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of]
  6
+      super + [:remote, :dependent, :primary_key, :inverse_of]
7 7
     end
8 8
 
9 9
     def self.define_accessors(model, reflection)
8  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -549,4 +549,12 @@ def test_has_one_autosave_with_primary_key_manually_set
549 549
     assert_not_nil author.post
550 550
     assert_equal author.post, post
551 551
   end
  552
+
  553
+  def test_has_one_relationship_cannot_have_a_counter_cache
  554
+    assert_raise(ArgumentError) do
  555
+      Class.new(ActiveRecord::Base) do
  556
+        has_one :thing, counter_cache: true
  557
+      end
  558
+    end
  559
+  end
552 560
 end
8  activerecord/test/cases/associations/has_one_through_associations_test.rb
@@ -315,4 +315,12 @@ def test_assigning_has_one_through_belongs_to_with_new_record_owner
315 315
   def test_has_one_through_with_custom_select_on_join_model_default_scope
316 316
     assert_equal clubs(:boring_club), members(:groucho).selected_club
317 317
   end
  318
+
  319
+  def test_has_one_through_relationship_cannot_have_a_counter_cache
  320
+    assert_raise(ArgumentError) do
  321
+      Class.new(ActiveRecord::Base) do
  322
+        has_one :thing, through: :other_thing, counter_cache: true
  323
+      end
  324
+    end
  325
+  end
318 326
 end

0 notes on commit 61ad8d5

Please sign in to comment.
Something went wrong with that request. Please try again.