Skip to content

Commit

Permalink
Merge pull request #46799 from Shopify/7-0-stable-build-singular-assoc
Browse files Browse the repository at this point in the history
Backport `create_association` fix to 7-0-stable
  • Loading branch information
byroot committed Dec 23, 2022
2 parents 18b5f24 + 76d0465 commit 83009f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Expand Up @@ -87,6 +87,10 @@ def set_new_record(record)
replace(record, false)
end

def replace_keys(record, force: false)
# Has one association doesn't have foreign keys to replace.
end

def remove_target!(method)
case method
when :delete
Expand Down
Expand Up @@ -54,11 +54,13 @@ def set_new_record(record)
end

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
record
reflection.klass.transaction do
record = build(attributes, &block)
saved = record.save
replace_keys(record, force: true)
raise RecordInvalid.new(record) if !saved && raise_error
record
end
end
end
end
Expand Down
Expand Up @@ -1428,6 +1428,15 @@ def test_should_set_foreign_key_on_create_association!
assert_equal firm.id, client.client_of
end

def test_should_set_foreign_key_on_create_association_with_unpersisted_owner
tagging = Tagging.new
tag = tagging.create_tag

assert_not_predicate tagging, :persisted?
assert_predicate tag, :persisted?
assert_equal tag.id, tagging.tag_id
end

def test_self_referential_belongs_to_with_counter_cache_assigning_nil
comment = Comment.create! post: posts(:thinking), body: "fuu"
comment.parent = nil
Expand Down

0 comments on commit 83009f9

Please sign in to comment.