public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
update_counters should update nil values.

This allows counter columns with default null instead of requiring default 0.

[#493 state:resolved]
miloops (author)
Thu Jun 26 09:46:33 -0700 2008
jeremy (committer)
Tue Jul 15 16:21:11 -0700 2008
commit  459e5817a513b95741b77af26771a6252a13d01f
tree    cc581c6fb6e67df99e75eaf8e3ccc4120ecc1d4f
parent  1edb5c85b58653a6fdc73ae1c6c63e317b466b27
...
828
829
830
831
 
832
833
834
...
828
829
830
 
831
832
833
834
0
@@ -828,7 +828,7 @@ module ActiveRecord #:nodoc:
0
       def update_counters(id, counters)
0
         updates = counters.inject([]) { |list, (counter_name, increment)|
0
           sign = increment < 0 ? "-" : "+"
0
-          list << "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} #{sign} #{increment.abs}"
0
+          list << "#{connection.quote_column_name(counter_name)} = COALESCE(#{connection.quote_column_name(counter_name)}, 0) #{sign} #{increment.abs}"
0
         }.join(", ")
0
         update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")
0
       end
...
19
20
21
 
22
23
24
...
75
76
77
78
 
79
80
81
...
130
131
132
133
 
134
135
136
...
614
615
616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
617
618
619
...
19
20
21
22
23
24
25
...
76
77
78
 
79
80
81
82
...
131
132
133
 
134
135
136
137
...
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
0
@@ -19,6 +19,7 @@ require 'models/warehouse_thing'
0
 require 'rexml/document'
0
 
0
 class Category < ActiveRecord::Base; end
0
+class Categorization < ActiveRecord::Base; end
0
 class Smarts < ActiveRecord::Base; end
0
 class CreditCard < ActiveRecord::Base
0
   class PinNumber < ActiveRecord::Base
0
@@ -75,7 +76,7 @@ class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
0
 end
0
 
0
 class BasicsTest < ActiveRecord::TestCase
0
-  fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors
0
+  fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations
0
 
0
   def test_table_exists
0
     assert !NonExistentTable.table_exists?
0
@@ -130,7 +131,7 @@ class BasicsTest < ActiveRecord::TestCase
0
 
0
   def test_read_attributes_before_type_cast
0
     category = Category.new({:name=>"Test categoty", :type => nil})
0
-    category_attrs = {"name"=>"Test categoty", "type" => nil}
0
+    category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
0
     assert_equal category_attrs , category.attributes_before_type_cast
0
   end
0
 
0
@@ -614,6 +615,22 @@ class BasicsTest < ActiveRecord::TestCase
0
     assert_equal -2, Topic.find(2).replies_count
0
   end
0
 
0
+  def test_update_counter
0
+    category = Category.first
0
+    assert_nil category.categorizations_count
0
+    assert_equal 2, category.categorizations.count
0
+
0
+    Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
0
+    category.reload
0
+    assert_not_nil category.categorizations_count
0
+    assert_equal 2, category.categorizations_count
0
+
0
+    Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
0
+    category.reload
0
+    assert_not_nil category.categorizations_count
0
+    assert_equal 4, category.categorizations_count
0
+  end
0
+
0
   def test_update_all
0
     assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
0
     assert_equal "bulk updated!", Topic.find(1).content
...
66
67
68
 
69
70
71
...
66
67
68
69
70
71
72
0
@@ -66,6 +66,7 @@ ActiveRecord::Schema.define do
0
   create_table :categories, :force => true do |t|
0
     t.string :name, :null => false
0
     t.string :type
0
+    t.integer :categorizations_count
0
   end
0
 
0
   create_table :categories_posts, :force => true, :id => false do |t|

Comments