diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-02-14 00:44:10 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-02-14 01:40:31 +0000 |
commit | 686418c65754701b9cdc213de4b6905f465fdc60 (patch) | |
tree | 52da5de76a53ad11e9b9085cdedc038f643a85be | |
parent | b93d2189c4cf370927b9796e0008034a5268214f (diff) | |
download | rails-686418c65754701b9cdc213de4b6905f465fdc60.tar.gz rails-686418c65754701b9cdc213de4b6905f465fdc60.tar.bz2 rails-686418c65754701b9cdc213de4b6905f465fdc60.zip |
Move create and create! next to build
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 42 |
1 files changed, 21 insertions, 21 deletions
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 # <tt>collection.size</tt> if it has. |