From 686418c65754701b9cdc213de4b6905f465fdc60 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 14 Feb 2011 00:44:10 +0000 Subject: Move create and create! next to build --- .../associations/association_collection.rb | 42 +++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'activerecord/lib/active_record/associations/association_collection.rb') diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index ff4c2b51ac..e391d9cce6 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -66,6 +66,27 @@ module ActiveRecord end end + def create(attrs = {}) + if attrs.is_a?(Array) + attrs.collect { |attr| create(attr) } + else + ensure_owner_is_persisted! + + transaction do + build_record(attrs) do |record| + yield(record) if block_given? + insert_record(record) + end + end + end + end + + def create!(attrs = {}, &block) + record = create(attrs, &block) + Array.wrap(record).each(&:save!) + record + end + # Add +records+ to this association. Returns +self+ so method calls may be chained. # Since << flattens its argument list and inserts each record, +push+ and +concat+ behave identically. def <<(*records) @@ -191,27 +212,6 @@ module ActiveRecord delete_or_destroy(records, :destroy) end - def create(attrs = {}) - if attrs.is_a?(Array) - attrs.collect { |attr| create(attr) } - else - ensure_owner_is_persisted! - - transaction do - build_record(attrs) do |record| - yield(record) if block_given? - insert_record(record) - end - end - end - end - - def create!(attrs = {}, &block) - record = create(attrs, &block) - Array.wrap(record).each(&:save!) - record - end - # Returns the size of the collection by executing a SELECT COUNT(*) # query if the collection hasn't been loaded, and calling # collection.size if it has. -- cgit v1.2.3