Git Product home page Git Product logo

Comments (5)

fatkodima avatar fatkodima commented on May 26, 2024

Can you create a reproduction test script using https://github.com/rails/rails/blob/main/guides/bug_report_templates/active_record.rb template?

from rails.

urmorae avatar urmorae commented on May 26, 2024

Sure thing, it breaks with sqlite as well. Seems like I cannot attach .rb files here so sry, test is in markdown:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails", "~> 7.1.0"
  gem "sqlite3", "~> 1.4"
end

require "active_record"
require "minitest/autorun"
require "logger"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :item_types, force: true do |t|
    t.string :item_type_nm, null: false
  end

  create_table :items, primary_key: [:item_type_id, :other_field] do |t|
    t.string :other_field, null: false
    t.references :item_type
  end
end

class ItemType < ActiveRecord::Base
  has_many :items
end

class Item < ActiveRecord::Base
  self.primary_key = [:item_type_id, :other_field]
  belongs_to :item_type
end

class BugTest < Minitest::Test
  def test_count_with_references
    Item.where(item_type_nm: 'a').includes(:item_type).references(:item_type).count
  end
end

from rails.

urmorae avatar urmorae commented on May 26, 2024

PS: .where condition is not actually relevant when reproducing the bug

from rails.

urmorae avatar urmorae commented on May 26, 2024

PS2: it seems to boil down to
lib/active_record/relation/calculations.rb

where

def select_for_count
  if select_values.present? # select_values here is assigned from table.primary_key i.e. [[:col1, :col2]]
    return select_values.first if select_values.one? # returns [:col1, :col2]
    select_values.join(", ")
  ...
end

from rails.

fatkodima avatar fatkodima commented on May 26, 2024

I was able to identify the problem and seems like found the solution. Will open a PR a bit later today.
But you are welcome to open a PR, if you want to try to fix it by yourself.

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.