aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-24 14:13:10 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-24 14:13:10 +0000
commit0d2db8a7d112488c93680e88c9beecfdea0a9db5 (patch)
tree3719345ceb657582cbddcbbd70118fbf35cdc4ed /activerecord/lib
parentb953ca8561ae05efbaedd84eec876a46d3b63ee1 (diff)
downloadrails-0d2db8a7d112488c93680e88c9beecfdea0a9db5.tar.gz
rails-0d2db8a7d112488c93680e88c9beecfdea0a9db5.tar.bz2
rails-0d2db8a7d112488c93680e88c9beecfdea0a9db5.zip
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
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/base.rb43
1 files changed, 42 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'"