New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preloading has_many through
associations loads duplicated records
#39073
Comments
I'm able to reproduce this, and it seems like unexpected behavior at first glance. Here's a slightly more minimal test case (using less relations) demonstrating the same phenomena: # frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
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 :meetings, force: true do |t|
end
create_table :participants, force: true do |t|
t.integer :meeting_id
end
create_table :agenda_items, force: true do |t|
t.integer :meeting_id
end
end
class Meeting < ActiveRecord::Base
has_many :participants
has_many :agenda_items
end
class Participant < ActiveRecord::Base
belongs_to :meeting
has_many :agenda_items, through: :meeting
end
class AgendaItem < ActiveRecord::Base
belongs_to :meeting
end
class BugTest < Minitest::Test
def test_association_stuff
meeting = Meeting.create
meeting.agenda_items << AgendaItem.create
3.times do
meeting.participants << Participant.create
end
association_without_agenda_items_preloaded = Participant.preload(:meeting)
association_with_agenda_items_preloaded = Participant.preload(:meeting, :agenda_items)
assert_equal association_without_agenda_items_preloaded[0].agenda_items.size,
association_with_agenda_items_preloaded[0].agenda_items.size
end
end |
I'm noticing that my test passes in Rails 5.2.4.2, but fails when ran against the current master branch. So, I'd categorize this as a bug/regression. |
It looks like Rails' behavior has changed back and forth several times. Using git-bisect, I believe 2847653 was the most recent commit to modify behavior to the current state (duplicates), and prior to that 5f9e050 (no duplicates). @bogdan, any interest in investigating? You've tackled a few preloading-related improvements. (If nobody else takes this on, I'll plan on picking this up in a few weeks, once I settle some other PRs I'm working on.) |
Steps to reproduce
I found two similar issues : #37891 and #39035.
Both use different types of associations and the internal ActiveRecord::Associations::Preloader
The example above uses only public API.
Expected behavior
In the example above, each parent and children have only one document or shared document
Preloading the parent should not change this behavior.
Actual behavior
Preloading the parent alongside shared documents duplicates the returned documents
System configuration
Rails version: 6.0.2.2
Ruby version: 2.6.3
The text was updated successfully, but these errors were encountered: