Permalink
Browse files

Move create and create! next to build

  • Loading branch information...
1 parent b93d218 commit 686418c65754701b9cdc213de4b6905f465fdc60 @jonleighton jonleighton committed Feb 14, 2011
Showing with 21 additions and 21 deletions.
  1. +21 −21 activerecord/lib/active_record/associations/association_collection.rb
@@ -66,6 +66,27 @@ def build(attributes = {}, &block)
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 @@ def destroy(*records)
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
# <tt>collection.size</tt> if it has.

0 comments on commit 686418c

Please sign in to comment.