Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a record to target before any callbacks loads the record #27442

Merged
merged 1 commit into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,23 @@ def add_to_target(record, skip_callbacks = false, &block)
def replace_on_target(record, index, skip_callbacks)
callback(:before_add, record) unless skip_callbacks

yield(record) if block_given?
begin
if index
record_was = target[index]
target[index] = record
else
target << record
end

if index
@target[index] = record
else
append_record(record)
yield(record) if block_given?
rescue
if index
target[index] = record_was
else
target.delete(record)
end

raise
end

callback(:after_add, record) unless skip_callbacks
Expand Down Expand Up @@ -502,10 +513,6 @@ def find_by_scan(*args)
load_target.select { |r| ids.include?(r.id.to_s) }
end
end

def append_record(record)
@target << record unless @target.include?(record)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ def find_target
def invertible_for?(record)
false
end

def append_record(record)
@target << record
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ def self.name

test "double insertion of new object to association when same association used in the after create callback of a new object" do
reset_callbacks(:save, Bulb) do
Bulb.after_save { |record| record.car.bulbs.to_a }
Bulb.after_save { |record| record.car.bulbs.load }
car = Car.create!
car.bulbs << Bulb.new
assert_equal 1, car.bulbs.size
Expand Down