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

Fix polymorphic assocation with nil type N+1 false alarm issue #70

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ def load_target
# always an N+1 query.
record.jit_n_plus_one_tracking ||= owner.jit_n_plus_one_tracking if record

if !jit_loaded && owner.jit_n_plus_one_tracking
if !jit_loaded && owner.jit_n_plus_one_tracking && !is_polymorphic_association_without_type

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit for positive logic and methods returning boolean results ending with a ?.

Reading "is not an association without a type" is a little confusing at first. Perhaps polymorphic_association_has_type? and checking self.klass.present? instead? What you have here isn't wrong though, so take it or leave it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's another approach to simplify the name but may complex the logic. So I will just keep it as is.

ActiveSupport::Notifications.publish("n_plus_one_query",
source: owner, association: reflection.name)
end
end
end
end

private def is_polymorphic_association_without_type
self.is_a?(ActiveRecord::Associations::BelongsToPolymorphicAssociation) && self.klass.nil?
end
end
end

Expand Down
19 changes: 6 additions & 13 deletions spec/lib/jit_preloader/preloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@
contact1.update!(contact_owner_type: nil, contact_owner_id: nil)
end

it "successfully load the rest of association values" do
it "successfully load the rest of association values and does not publish a n+1 notification" do
contacts = Contact.jit_preload.to_a
expect(contacts.first.contact_owner).to eq(nil)
ActiveSupport::Notifications.subscribed(callback, "n_plus_one_query") do
expect(contacts.first.contact_owner).to eq(nil)
end

expect(source_map).to eql({})

expect do
contacts.first.contact_owner
Expand All @@ -184,17 +188,6 @@
expect(contacts.second.contact_owner).to eq(ContactOwner.first)
expect(contacts.third.contact_owner).to eq(Address.first)
end

it "publish N+1 notification due to polymorphic nil type" do
contacts = Contact.jit_preload.to_a

ActiveSupport::Notifications.subscribed(callback, "n_plus_one_query") do
contacts.first.contact_owner
end

expect_source_map = { contacts.first => [:contact_owner] }
expect(source_map).to eql(expect_source_map)
end
end
end
end
Expand Down