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

Code before:

  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.
    Bird.delete_all
    Parrot.delete_all
    @ship.delete
    @pirate.delete
    Cpk::Book.delete_all
    Cpk::Order.delete_all
  end

  # reload
  def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
    @pirate.mark_for_destruction

Review comment (suggestion):
On main:
![Screenshot 2025-01-14 at 7 12 18 pm](https://github.com/user-attachments/assets/e53ae3f1-8357-490b-a1c7-b9d4a260693a)

Code after:

  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.
    Bird.delete_all
    Parrot.delete_all
    @ship.delete
    @pirate.delete
    Cpk::Book.delete_all
    Cpk::Order.delete_all
  end

  # reload
  def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
    @pirate.mark_for_destruction