Skip to content
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

Rails 6 sometimes loads associations twice, causing duplicated results #32

Open
fimmtiu opened this issue Jul 7, 2020 · 0 comments
Open

Comments

@fimmtiu
Copy link
Contributor

fimmtiu commented Jul 7, 2020

If multiple records in a collection have the same record set for a belongs_to, which itself has a has_many association, the has_many association will return duplicate records. When using ActiveRecord's preload, there are no duplicate records.

Example:

[1] pry> cp = EmailAddress.jit_preload.to_a.first.contact.phone_numbers
=> [#<PhoneNumber:0x00007fa15315b4f8 id: 1, contact_id: 1, phone: "123">,
    #<PhoneNumber:0x00007fa15315b4f8 id: 1, contact_id: 1, phone: "123">]
[2] pry> EmailAddress.preload(contact: :phone_numbers).to_a.first.contact.phone_numbers
=> [#<PhoneNumber:0x00007fa1436fe870 id: 1, contact_id: 1, phone: "123">]

This also results in AR making two identical queries for the phone_numbers instead of only one.

Here's a spec which replicates the problem: https://github.com/clio/jit_preloader/pull/31/files. Seems to only happen under Rails 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant