From 0d2db8a7d112488c93680e88c9beecfdea0a9db5 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 24 Jan 2005 14:13:10 +0000 Subject: Added Base.update_collection that can update an array of id/attribute pairs, such as the ones produced by the recent added support for automatic id-based indexing for lists of items #526 [Duane Johnson] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@496 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 43 +++++++++++++++++++++++++++++++++- activerecord/test/base_test.rb | 17 ++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 51ee251871..cb13f78f7d 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -372,7 +372,48 @@ module ActiveRecord #:nodoc: add_conditions!(sql, conditions) return connection.update(sql, "#{name} Update") end - + + # Updates several records at a time using the pattern of a hash that contains id => {attributes} pairs as contained in +id_and_attributes_pairs+. + # If there are certain conditions that must be met in order for the update to occur, an optional block + # containing a conditional statement may be used. Example: + # Person.update_collection { 23 => { "first_name" => "John", "last_name" => "Peterson" }, + # 25 => { "first_name" => "Duane", "last_name" => "Johnson" } } + # + # # Updates only those records whose first name begins with 'duane' or 'Duane' + # Person.update_collection @params['people'] do |activerecord_object, new_attributes| + # activerecord_object.first_name =~ /^[dD]uane.*/ + # end + # + # The conditional block may also be of use when you have more than one kind of key in the +id_and_attributes_pairs+ hash. + # For example, if you have a view that contains form elements of both existing and new records, you might end up with + # a hash that looks like this: + # @params['people'] = { "1" => { "first_name" => "Bob", "last_name" => "Schilling" }, + # "2" => { "first_name" => "Joe", "last_name" => "Tycoon" }, + # "new_person" => { "first_name" => "Mary", "last_name" => "Smith" } } + # In such a case, you could discriminate against 'updating' the new_person record with the following code: + # Person.update_collection(@params['people']) { |ar, attrs| ar.id.to_i > 0 } + # + # This works because the to_i method converts all non-integer strings to 0. + def update_collection(id_and_attributes_pairs) + updated_records = Array.new + + transaction do + records = find(id_and_attributes_pairs.keys) + id_and_attributes_pairs.each do |id, attrs| + record = records.select { |r| r.id.to_s == id }.first + + # Update each record unless the closure is false + if (!block_given? || (block_given? && yield(record, attrs))) + record.update_attributes(attrs) + updated_records << record + end + end + end + + return updated_records + end + + # Destroys the objects for all the records that matches the +condition+ by instantiating each object and calling # the destroy method. Example: # Person.destroy_all "last_login < '2004-04-04'" diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 02690b7396..2119785caf 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -292,6 +292,23 @@ class BasicsTest < Test::Unit::TestCase assert_equal "bulk updated again!", Topic.find(2).content end + def test_update_collection + ids_and_attributes = { "1" => { "content" => "1 updated" }, "2" => { "content" => "2 updated" } } + updated = Topic.update_collection(ids_and_attributes) + + assert_equal 2, updated.size + assert_equal "1 updated", Topic.find(1).content + assert_equal "2 updated", Topic.find(2).content + + ids_and_attributes["1"]["content"] = "one updated" + ids_and_attributes["2"]["content"] = "two updated" + updated = Topic.update_collection(ids_and_attributes) { |ar, attrs| ar.id == 1 } + + assert_equal 1, updated.size + assert_equal "one updated", Topic.find(1).content + assert_equal "2 updated", Topic.find(2).content + end + def test_delete_all assert_equal 2, Topic.delete_all end -- cgit v1.2.3