mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Initialization block is a part of build_record
Should be done before `before_add` callbacks.
This commit is contained in:
parent
69d91cd6a6
commit
c256d02f01
7 changed files with 22 additions and 14 deletions
|
@ -275,6 +275,7 @@ module ActiveRecord
|
|||
def build_record(attributes)
|
||||
reflection.build_association(attributes) do |record|
|
||||
initialize_attributes(record, attributes)
|
||||
yield(record) if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -105,9 +105,7 @@ module ActiveRecord
|
|||
if attributes.is_a?(Array)
|
||||
attributes.collect { |attr| build(attr, &block) }
|
||||
else
|
||||
add_to_target(build_record(attributes)) do |record|
|
||||
yield(record) if block_given?
|
||||
end
|
||||
add_to_target(build_record(attributes, &block))
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -361,8 +359,7 @@ module ActiveRecord
|
|||
attributes.collect { |attr| _create_record(attr, raise, &block) }
|
||||
else
|
||||
transaction do
|
||||
add_to_target(build_record(attributes)) do |record|
|
||||
yield(record) if block_given?
|
||||
add_to_target(build_record(attributes, &block)) do |record|
|
||||
insert_record(record, true, raise) {
|
||||
@_was_loaded = loaded?
|
||||
@association_ids = nil
|
||||
|
|
|
@ -90,7 +90,7 @@ module ActiveRecord
|
|||
def build_record(attributes)
|
||||
ensure_not_nested
|
||||
|
||||
record = super(attributes)
|
||||
record = super
|
||||
|
||||
inverse = source_reflection.inverse_of
|
||||
if inverse
|
||||
|
|
|
@ -106,7 +106,7 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def _create_record(attributes, raise_error = false)
|
||||
def _create_record(attributes, raise_error = false, &block)
|
||||
unless owner.persisted?
|
||||
raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
|
||||
end
|
||||
|
|
|
@ -17,9 +17,8 @@ module ActiveRecord
|
|||
replace(record)
|
||||
end
|
||||
|
||||
def build(attributes = {})
|
||||
record = build_record(attributes)
|
||||
yield(record) if block_given?
|
||||
def build(attributes = {}, &block)
|
||||
record = build_record(attributes, &block)
|
||||
set_new_record(record)
|
||||
record
|
||||
end
|
||||
|
@ -62,9 +61,8 @@ module ActiveRecord
|
|||
replace(record)
|
||||
end
|
||||
|
||||
def _create_record(attributes, raise_error = false)
|
||||
record = build_record(attributes)
|
||||
yield(record) if block_given?
|
||||
def _create_record(attributes, raise_error = false, &block)
|
||||
record = build_record(attributes, &block)
|
||||
saved = record.save
|
||||
set_new_record(record)
|
||||
raise RecordInvalid.new(record) if !saved && raise_error
|
||||
|
|
|
@ -114,7 +114,7 @@ module ActiveRecord
|
|||
attributes[inverse.foreign_key] = target.id
|
||||
end
|
||||
|
||||
super(attributes)
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -737,6 +737,18 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
|
|||
[:added, :before, "Roger"],
|
||||
[:added, :after, "Roger"]
|
||||
], log.last(4)
|
||||
|
||||
post.people_with_callbacks.build { |person| person.first_name = "Ted" }
|
||||
assert_equal [
|
||||
[:added, :before, "Ted"],
|
||||
[:added, :after, "Ted"]
|
||||
], log.last(2)
|
||||
|
||||
post.people_with_callbacks.create { |person| person.first_name = "Sam" }
|
||||
assert_equal [
|
||||
[:added, :before, "Sam"],
|
||||
[:added, :after, "Sam"]
|
||||
], log.last(2)
|
||||
end
|
||||
|
||||
def test_dynamic_find_should_respect_association_include
|
||||
|
|
Loading…
Reference in a new issue