提交 0ceb21e3 编写于 作者: J José Valim

Revert "b9ea751d".

Wrapping each Record.associations.build in a transaction is going to
make several unneeded queries. Reverting this commit also fixes #479.
上级 7c353d57
......@@ -94,7 +94,14 @@ def last(*args)
end
def build(attributes = {}, options = {}, &block)
build_or_create(:build, attributes, options, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| build(attr, options, &block) }
else
add_to_target(build_record(attributes, options)) do |record|
yield(record) if block_given?
set_owner_attributes(record)
end
end
end
def create(attributes = {}, options = {}, &block)
......@@ -102,7 +109,16 @@ def create(attributes = {}, options = {}, &block)
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
end
build_or_create(:create, attributes, options, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
transaction do
add_to_target(build_record(attributes, options)) do |record|
yield(record) if block_given?
insert_record(record)
end
end
end
end
def create!(attrs = {}, options = {}, &block)
......@@ -337,20 +353,18 @@ def load_target
end
def add_to_target(record)
transaction do
callback(:before_add, record)
yield(record) if block_given?
if options[:uniq] && index = @target.index(record)
@target[index] = record
else
@target << record
end
callback(:before_add, record)
yield(record) if block_given?
callback(:after_add, record)
set_inverse_instance(record)
if options[:uniq] && index = @target.index(record)
@target[index] = record
else
@target << record
end
callback(:after_add, record)
set_inverse_instance(record)
record
end
......@@ -403,19 +417,6 @@ def merge_target_lists(loaded, existing)
end + existing
end
def build_or_create(method, attributes, options)
records = Array.wrap(attributes).map do |attrs|
record = build_record(attrs, options)
add_to_target(record) do
yield(record) if block_given?
insert_record(record) if method == :create
end
end
attributes.is_a?(Array) ? records : records.first
end
# Do the relevant stuff to insert the given record into the association collection.
def insert_record(record, validate = true)
raise NotImplementedError
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册