Skip to content

Commit

Permalink
matched insert_all scope attribute precedence to create
Browse files Browse the repository at this point in the history
  • Loading branch information
kobsy committed Jun 1, 2022
1 parent 920fcce commit c7075b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
12 changes: 8 additions & 4 deletions activerecord/lib/active_record/insert_all.rb
Expand Up @@ -23,8 +23,11 @@ def initialize(model, inserts, on_duplicate:, update_only: nil, returning: nil,
@keys = @inserts.first.keys.map(&:to_s)

if model.scope_attributes?
@scope_attributes = model.scope_attributes
@keys |= @scope_attributes.keys
reflection = model.current_scope.proxy_association.reflection if model.current_scope.respond_to?(:proxy_association)
association_keys = [ reflection&.foreign_key, reflection&.type ].compact
@association_attributes = model.scope_attributes.slice(*association_keys)
@scope_attributes = model.scope_attributes.except(*association_keys)
@keys |= @scope_attributes.keys | @association_attributes.keys
end
@keys = @keys.to_set

Expand Down Expand Up @@ -64,7 +67,8 @@ def update_duplicates?
def map_key_with_value
inserts.map do |attributes|
attributes = attributes.stringify_keys
attributes.merge!(scope_attributes) if scope_attributes
attributes.merge!(association_attributes) if association_attributes
attributes.reverse_merge!(scope_attributes) if scope_attributes
attributes.reverse_merge!(timestamps_for_create) if record_timestamps?

verify_attributes(attributes)
Expand All @@ -89,7 +93,7 @@ def keys_including_timestamps
end

private
attr_reader :scope_attributes
attr_reader :association_attributes, :scope_attributes

def has_attribute_aliases?(attributes)
attributes.keys.any? { |attribute| model.attribute_alias?(attribute) }
Expand Down
22 changes: 22 additions & 0 deletions activerecord/test/cases/insert_all_test.rb
Expand Up @@ -646,6 +646,16 @@ def test_insert_all_on_relation_precedence
end
end

def test_insert_all_on_scope_precedence
author = Author.create!(name: "Jimmy")

assert_difference "author.books.count", +1 do
assert_difference "author.books.written.count", +1 do
author.books.published.insert_all!([{ name: "My little book", isbn: "1974522598", status: :written }])
end
end
end

def test_insert_all_create_with
assert_difference "Book.where(format: 'X').count", +2 do
Book.create_with(format: "X").insert_all!([ { name: "A" }, { name: "B" } ])
Expand Down Expand Up @@ -678,6 +688,18 @@ def test_upsert_all_on_relation_precedence
end
end

def test_upsert_all_on_scope_precedence
skip unless supports_insert_on_duplicate_update?

author = Author.create!(name: "Jimmy")

assert_difference "author.books.count", +1 do
assert_difference "author.books.written.count", +1 do
author.books.published.upsert_all([{ name: "My little book", isbn: "1974522598", status: :written }])
end
end
end

def test_upsert_all_create_with
skip unless supports_insert_on_duplicate_update?

Expand Down

0 comments on commit c7075b2

Please sign in to comment.