mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
update_counters should update nil values.
This allows counter columns with default null instead of requiring default 0. [#493 state:resolved]
This commit is contained in:
@@ -828,7 +828,7 @@ module ActiveRecord #:nodoc:
|
||||
def update_counters(id, counters)
|
||||
updates = counters.inject([]) { |list, (counter_name, increment)|
|
||||
sign = increment < 0 ? "-" : "+"
|
||||
list << "#{connection.quote_column_name(counter_name)} = #{connection.quote_column_name(counter_name)} #{sign} #{increment.abs}"
|
||||
list << "#{connection.quote_column_name(counter_name)} = COALESCE(#{connection.quote_column_name(counter_name)}, 0) #{sign} #{increment.abs}"
|
||||
}.join(", ")
|
||||
update_all(updates, "#{connection.quote_column_name(primary_key)} = #{quote_value(id)}")
|
||||
end
|
||||
|
||||
@@ -19,6 +19,7 @@ require 'models/warehouse_thing'
|
||||
require 'rexml/document'
|
||||
|
||||
class Category < ActiveRecord::Base; end
|
||||
class Categorization < ActiveRecord::Base; end
|
||||
class Smarts < ActiveRecord::Base; end
|
||||
class CreditCard < ActiveRecord::Base
|
||||
class PinNumber < ActiveRecord::Base
|
||||
@@ -75,7 +76,7 @@ class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base
|
||||
end
|
||||
|
||||
class BasicsTest < ActiveRecord::TestCase
|
||||
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors
|
||||
fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations
|
||||
|
||||
def test_table_exists
|
||||
assert !NonExistentTable.table_exists?
|
||||
@@ -130,7 +131,7 @@ class BasicsTest < ActiveRecord::TestCase
|
||||
|
||||
def test_read_attributes_before_type_cast
|
||||
category = Category.new({:name=>"Test categoty", :type => nil})
|
||||
category_attrs = {"name"=>"Test categoty", "type" => nil}
|
||||
category_attrs = {"name"=>"Test categoty", "type" => nil, "categorizations_count" => nil}
|
||||
assert_equal category_attrs , category.attributes_before_type_cast
|
||||
end
|
||||
|
||||
@@ -614,6 +615,22 @@ class BasicsTest < ActiveRecord::TestCase
|
||||
assert_equal -2, Topic.find(2).replies_count
|
||||
end
|
||||
|
||||
def test_update_counter
|
||||
category = Category.first
|
||||
assert_nil category.categorizations_count
|
||||
assert_equal 2, category.categorizations.count
|
||||
|
||||
Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
|
||||
category.reload
|
||||
assert_not_nil category.categorizations_count
|
||||
assert_equal 2, category.categorizations_count
|
||||
|
||||
Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
|
||||
category.reload
|
||||
assert_not_nil category.categorizations_count
|
||||
assert_equal 4, category.categorizations_count
|
||||
end
|
||||
|
||||
def test_update_all
|
||||
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
|
||||
assert_equal "bulk updated!", Topic.find(1).content
|
||||
|
||||
@@ -66,6 +66,7 @@ ActiveRecord::Schema.define do
|
||||
create_table :categories, :force => true do |t|
|
||||
t.string :name, :null => false
|
||||
t.string :type
|
||||
t.integer :categorizations_count
|
||||
end
|
||||
|
||||
create_table :categories_posts, :force => true, :id => false do |t|
|
||||
|
||||
Reference in New Issue
Block a user