Skip to content

Fix N+1 query in Node#tagnames_as_classes#11862

Open
apoorvdarshan wants to merge 1 commit intopubliclab:mainfrom
apoorvdarshan:fix-n-plus-1-tagnames-as-classes
Open

Fix N+1 query in Node#tagnames_as_classes#11862
apoorvdarshan wants to merge 1 commit intopubliclab:mainfrom
apoorvdarshan:fix-n-plus-1-tagnames-as-classes

Conversation

@apoorvdarshan
Copy link

@apoorvdarshan apoorvdarshan commented Feb 17, 2026

Summary

  • Eliminates N+1 query in Node#tagnames_as_classes by replacing Node.select([:nid]).find(id).tagnames with a direct Tag.joins(:node_tag).pluck(:name) call
  • Reduces two queries per node (Node.find + tag association load) to a single efficient pluck query
  • No change in behavior — returns the same tag class strings used in note listing views

Details

The previous implementation re-fetched the node via Node.find(id) to bypass a potentially filtered tag association (caused by joins in the calling context). This created an N+1 problem when rendering lists of notes.

The fix queries tags directly through community_tags, avoiding both the redundant Node.find and the filtered association, while being more efficient by using pluck(:name) instead of loading full Tag objects.

Test plan

  • Existing test test/unit/node_test.rb line 291 validates output: 'tag-test tag-awesome tag-spectrometer tag-activity-spectrometer tag-sub-tag'
  • Verify note listing pages (/notes, /dashboard) render tag CSS classes correctly

Closes #11853

Replace Node.find(id) re-query with a direct Tag.joins(:node_tag).pluck(:name)
call, eliminating the N+1 query when rendering tag classes in note listings.
This reduces two queries per node (Node.find + tag association load) to a
single efficient pluck query.

Closes publiclab#11853
@apoorvdarshan apoorvdarshan force-pushed the fix-n-plus-1-tagnames-as-classes branch from 40c54e8 to 75e176e Compare February 17, 2026 16:14
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

Successfully merging this pull request may close these issues.

Avoid N+1 query in Tag#tagnames_as_classes

1 participant