Created
October 27, 2025 13:51
-
-
Save jturkel/a1eb8b31bdf86c732bdd43e5419e01ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| begin | |
| require 'bundler/inline' | |
| rescue LoadError => e | |
| $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
| raise e | |
| end | |
| gemfile(true) do | |
| source 'https://rubygems.org' | |
| gem 'rails', '~> 8.0' | |
| gem 'sqlite3' | |
| end | |
| require 'active_record' | |
| require 'minitest/autorun' | |
| require 'logger' | |
| puts "Using ActiveRecord #{ActiveRecord::VERSION::STRING}" | |
| ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
| ActiveRecord::Base.logger = Logger.new(STDOUT) | |
| # Schema | |
| ActiveRecord::Schema.define do | |
| create_table(:sections, force: true) do |t| | |
| t.string :name | |
| end | |
| create_table(:section_contents, force: true) do |t| | |
| t.references :section | |
| t.references :article | |
| end | |
| create_table(:articles, force: true) do |t| | |
| t.string :title | |
| t.string :status | |
| end | |
| end | |
| # Models | |
| class Section < ActiveRecord::Base | |
| has_many :section_contents, dependent: :destroy | |
| has_many :published_articles, -> { where(status: 'published') }, through: :section_contents, source: :article | |
| has_many :draft_articles, -> { where(status: 'draft') }, through: :section_contents, source: :article | |
| has_many :all_articles, through: :section_contents, source: :article | |
| end | |
| class SectionContent < ActiveRecord::Base | |
| belongs_to :section | |
| belongs_to :article | |
| end | |
| class Article < ActiveRecord::Base | |
| end | |
| class BugTest < Minitest::Test | |
| def test_bug | |
| section1 = Section.create!(name: 'section1') | |
| section1_published1 = section1.section_contents.create!(article: Article.create!(title: 'section1-published1', status: 'published')) | |
| section1_published2 = section1.section_contents.create!(article: Article.create!(title: 'section1-published2', status: 'published')) | |
| section1_draft1 = section1.section_contents.create!(article: Article.create!(title: 'section1-draft1', status: 'draft')) | |
| # section2 = Section.create!(name: 'section2') | |
| # section2_published1 = section2.section_contents.create!(article: Article.create!(title: 'section2-published1', status: 'published')) | |
| # section2_draft1 = section2.section_contents.create!(article: Article.create!(title: 'section2-draft1', status: 'draft')) | |
| # section2_draft2 = section2.section_contents.create!(article: Article.create!(title: 'section2-draft2', status: 'draft')) | |
| # Bug only reproduces if the unfiltered association is loaded first | |
| section = Section.includes(:all_articles, :published_articles).find(section1.id) | |
| # Works fine if we load published_articles first | |
| # section = Section.includes(:published_articles, :all_articles).find(section1.id) | |
| # Works fine if we don't eager load | |
| # section = Section.find(section1.id) | |
| section.section_contents.to_a | |
| assert_equal(3, section.all_articles.to_a.size) | |
| assert_equal(2, section.published_articles.to_a.size) | |
| # assert_equal(1, section.draft_articles.to_a.size) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment