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

Code before:
    assert_equal [part.id], ShipPart.joins(:trinkets).ids
    assert_async_equal [part.id], ShipPart.joins(:trinkets).async_ids
  end

  def test_ids_with_eager_load
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.eager_load(:contracts).ids.sort
  end

  def test_ids_with_preload
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.preload(:contracts).ids.sort
  end

  def test_ids_with_includes
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.includes(:contracts).ids.sort
  end

  def test_ids_with_includes_and_scope
    scoped_ids = [1, 2]
    company = Company.where(id: scoped_ids).first
    5.times { company.contracts.create! }
    assert_equal Company.where(id: scoped_ids).map(&:id).sort, Company.includes(:contracts).where(id: scoped_ids).ids.sort
  end

  def test_ids_with_includes_and_table_scope
    company = Company.first
    company.contracts.create!
    assert_equal [company.id], Company.includes(:contracts).where("contracts.id" => company.contracts.first).ids
  end

  def test_ids_on_loaded_relation_with_includes_and_table_scope
    company = Company.first
    company.contracts.create!
    loaded_companies = Company.includes(:contracts).where("contracts.id" => company.contracts.first).load
    assert_queries(0) do
      assert_equal [company.id], loaded_companies.ids
    end
  end

  def test_ids_with_includes_limit_and_empty_result
    assert_equal [], Topic.includes(:replies).limit(0).ids
    assert_equal [], Topic.includes(:replies).limit(1).where("0 = 1").ids
  end

  def test_ids_with_includes_offset
    assert_equal [5], Topic.includes(:replies).order(:id).offset(4).ids

Review comment (suggestion):
Before the fix:
<img width="1083" alt="Screenshot 2023-05-01 at 9 14 51 am" src="https://user-images.githubusercontent.com/54629302/235408184-5235f577-d96c-4640-a41b-209fbe4007eb.png">

Code after:
    assert_equal [part.id], ShipPart.joins(:trinkets).ids
    assert_async_equal [part.id], ShipPart.joins(:trinkets).async_ids
  end

  def test_ids_with_eager_load
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.eager_load(:contracts).ids.sort
  end

  def test_ids_with_preload
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.preload(:contracts).ids.sort
  end

  def test_ids_with_includes
    company = Company.first
    5.times { company.contracts.create! }
    assert_equal Company.all.map(&:id).sort, Company.all.includes(:contracts).ids.sort
  end

  def test_ids_with_includes_and_non_primary_key_order
    rating = 1
    Company.all.each { |company| company.update!(rating: rating += 1) }
    assert_equal Company.all.sort_by(&:rating).map(&:id), Company.includes(:comments).order(:rating).ids
  end

  def test_ids_with_includes_and_scope
    scoped_ids = [1, 2]
    company = Company.where(id: scoped_ids).first
    5.times { company.contracts.create! }
    assert_equal Company.where(id: scoped_ids).map(&:id).sort, Company.includes(:contracts).where(id: scoped_ids).ids.sort
  end

  def test_ids_with_includes_and_table_scope
    company = Company.first
    company.contracts.create!
    assert_equal [company.id], Company.includes(:contracts).where("contracts.id" => company.contracts.first).ids
  end

  def test_ids_on_loaded_relation_with_includes_and_table_scope
    company = Company.first
    company.contracts.create!
    loaded_companies = Company.includes(:contracts).where("contracts.id" => company.contracts.first).load
    assert_queries(0) do
      assert_equal [company.id], loaded_companies.ids
    end
  end

  def test_ids_with_includes_limit_and_empty_result