Permalink
Browse files

Refactor create_record and build_record in AssociationCollection

  • Loading branch information...
1 parent ffa5767 commit 7f5fcc0785e28175b1a54971e5adc34ecd50787d @jonleighton jonleighton committed with tenderlove Dec 17, 2010
Showing with 5 additions and 17 deletions.
  1. +5 −17 activerecord/lib/active_record/associations/association_collection.rb
@@ -487,32 +487,20 @@ def save_record(record, force, validate)
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)

0 comments on commit 7f5fcc0

Please sign in to comment.