Repository: rails/rails
File: activerecord/test/cases/autosave_association_test.rb (Ruby)

Code before:
    assert_not_nil post.author_id
  end

  def test_autosave_new_record_with_after_create_callback_and_habtm_association
    post = PostWithAfterCreateCallback.new(title: "Captain Murphy", body: "is back")
    post.comments.build(body: "foo")
    post.categories.build(name: "bar")
    post.save!

    assert_equal 1, post.categories.reload.length
  end

  FamilyLoadingMiddleAndThroughRecordsBeforeSave = Class.new(Family) do
    before_save do
      family_trees.map(&:member) + members
    end
  end

  def test_autosave_new_record_with_hmt_and_middle_record_built_by_parent
    family = FamilyLoadingMiddleAndThroughRecordsBeforeSave.new
    family_tree = family.family_trees.build
    member = family_tree.build_member
    family.save!
    family.reload

    assert_equal 1, family.family_trees.size
    assert_equal 1, family.members.size
  end

  def test_autosave_new_record_with_hmt_and_middle_record_built_by_through_record
    family = FamilyLoadingMiddleAndThroughRecordsBeforeSave.new
    member = family.members.build
    family_tree = family.family_trees.build(member: member)
    family.save!
    family.reload

    assert_equal 1, family.family_trees.size
    assert_equal 1, family.members.size
  end
end

class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
  self.use_transactional_tests = false

  setup do
    @pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
    @ship = @pirate.create_ship(name: "Nights Dirty Lightning")
  end

  teardown do
    # We are running without transactional tests and need to cleanup.

Review comment (suggestion):
On main:
![Screenshot 2025-01-14 at 7 11 34 pm](https://github.com/user-attachments/assets/0fd37ad1-d14f-4484-97cb-7840f6e4dd4f)

Code after:
    assert_not_nil post.author_id
  end

  def test_autosave_new_record_with_after_create_callback_and_habtm_association
    post = PostWithAfterCreateCallback.new(title: "Captain Murphy", body: "is back")
    post.comments.build(body: "foo")
    post.categories.build(name: "bar")
    post.save!

    assert_equal 1, post.categories.reload.length
  end

  FamilyLoadingMiddleAndThroughRecordsBeforeSave = Class.new(Family) do
    before_save do
      family_trees.map(&:member) + members
    end
  end

  def test_autosave_new_record_with_hmt_and_middle_record_built_by_parent
    family = FamilyLoadingMiddleAndThroughRecordsBeforeSave.new
    family_tree = family.family_trees.build
    family_tree.build_member
    family.save!
    family.reload

    assert_equal 1, family.family_trees.size
    assert_equal 1, family.members.size
  end

  def test_autosave_new_record_with_hmt_and_middle_record_built_by_through_record
    family = FamilyLoadingMiddleAndThroughRecordsBeforeSave.new
    member = family.members.build
    family.family_trees.build(member: member)
    family.save!
    family.reload

    assert_equal 1, family.family_trees.size
    assert_equal 1, family.members.size
  end
end

class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
  self.use_transactional_tests = false

  setup do
    @pirate = Pirate.create(catchphrase: "Don' botharrr talkin' like one, savvy?")
    @ship = @pirate.create_ship(name: "Nights Dirty Lightning")
  end

  teardown do
    # We are running without transactional tests and need to cleanup.