diff options
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 43 | ||||
-rwxr-xr-x | activerecord/test/base_test.rb | 17 |
2 files changed, 59 insertions, 1 deletions
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 |