From efa81dad5158eb61243d00811e4664248dd2c167 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 25 Jan 2005 12:45:01 +0000 Subject: Added the option of supplying an array of ids and attributes to Base#update, so that multiple records can be updated at once (inspired by #526/Duane Johnson). Added the option of supplying an array of attributes to Base#create, so that multiple records can be created at once. Added that Base#delete and Base#destroy both can take an array of ids to delete/destroy #336. Added that has_many association build and create methods can take arrays of record data like Base#create and Base#build to build/create multiple records at once. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@504 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_record/associations/association_collection.rb | 10 +++++++--- .../lib/active_record/associations/has_many_association.rb | 14 +++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 9507822f40..cf1d0ecefc 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -53,9 +53,13 @@ module ActiveRecord def create(attributes = {}) # Can't use Base.create since the foreign key may be a protected attribute. - record = build(attributes) - record.save unless @owner.new_record? - record + if attributes.is_a?(Array) + attributes.collect { |attr| create(attr) } + else + record = build(attributes) + record.save unless @owner.new_record? + record + end end # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index f3ab6bcdab..0d72b9b0c5 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -9,11 +9,15 @@ module ActiveRecord end def build(attributes = {}) - load_target - record = @association_class.new(attributes) - record[@association_class_primary_key_name] = @owner.id unless @owner.new_record? - @target << record - record + if attributes.is_a?(Array) + attributes.collect { |attr| create(attr) } + else + load_target + record = @association_class.new(attributes) + record[@association_class_primary_key_name] = @owner.id unless @owner.new_record? + @target << record + record + end end def find_all(runtime_conditions = nil, orderings = nil, limit = nil, joins = nil) -- cgit v1.2.3