diff options
author | Jon Leighton <j@jonathanleighton.com> | 2010-12-17 09:56:47 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-20 13:56:04 -0800 |
commit | 7f5fcc0785e28175b1a54971e5adc34ecd50787d (patch) | |
tree | 1616c01fdf65d9d5783b0e0f8575289bf0130915 /activerecord/lib/active_record/associations | |
parent | ffa57671bb664a715eb2eebaa7476c747abf0fb1 (diff) | |
download | rails-7f5fcc0785e28175b1a54971e5adc34ecd50787d.tar.gz rails-7f5fcc0785e28175b1a54971e5adc34ecd50787d.tar.bz2 rails-7f5fcc0785e28175b1a54971e5adc34ecd50787d.zip |
Refactor create_record and build_record in AssociationCollection
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 22 |
1 files changed, 5 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 3f80133299..18d05aa133 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -487,32 +487,20 @@ module ActiveRecord force ? record.save! : record.save(:validate => validate) end - def create_record(attrs) + def create_record(attrs, &block) ensure_owner_is_persisted! - attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) - create_scope = @scope[:create].merge(scoped.where_values_hash || {}) - transaction do - record = with_scope(:create => create_scope) do - @reflection.build_association(attrs) - end - if block_given? - add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } - else - add_record_to_target_with_callbacks(record) + with_scope(:create => @scope[:create].merge(scoped.where_values_hash || {})) do + build_record(attrs, &block) end end end - def build_record(attrs) + def build_record(attrs, &block) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) record = @reflection.build_association(attrs) - if block_given? - add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } - else - add_record_to_target_with_callbacks(record) - end + add_record_to_target_with_callbacks(record, &block) end def remove_records(*records) |