You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"gem"rails"# Uncomment the following line to test it against the fix provided at https://github.com/rails/rails/pull/50389# gem "rails", github: "joshuay03/rails", branch: "strict-loading-not-raising-on-nested-has-many-with-query-methods"gem"sqlite3"endrequire"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.definedocreate_table:posts,force: truedo |t|
endcreate_table:comments,force: truedo |t|
t.integer:post_idendendclassPost < ActiveRecord::Basehas_many:commentsendclassComment < ActiveRecord::Basebelongs_to:postendclassBugTest < Minitest::Testdeftest_association_stuffpost=Post.create!post.comments << Comment.create!posts=Post.all.strict_loading!assert_raises(ActiveRecord::StrictLoadingViolationError)doposts.first.comments.to_aendassert_raises(ActiveRecord::StrictLoadingViolationError)doposts.first.comments.pluck(:id)endendend
Expected behavior
The second assertion should also pass. Calling posts.first.comments.pluck(:id) should also raise an ActiveRecord::StrictLoadingViolationError just as the to_a does.
Actual behavior
Calling posts.first.comments.pluck(:id) does not raise an error and calls N+1 queries instead
System configuration
Rails version:
7.1.3.2 Ruby version:
3.2.2
There is a similar issue raised #49520 and I also referenced the provided fix in #50389 in the POC and the issue still remains. Hence raising this issue as separate.
The text was updated successfully, but these errors were encountered:
dombesz
changed the title
Calling pluck on an association of Post.all.strict_loading! will execute the N+1 instead of raising ActiveRecord::StrictLoadingViolationError
Calling pluck on a strict_loading! association will execute the N+1 instead of raising ActiveRecord::StrictLoadingViolationErrorApr 9, 2024
Hey @nikhil-dronamraju I've been reviewing the issue and realized I hadn't shouted - apologies if you've already started looking into this 🙏
I'm nearly ready to submit my PR to address the fix. However, if you've already prepared a PR for the same issue, just let me know, and I'll hold off on mine.
Steps to reproduce
Expected behavior
The second assertion should also pass. Calling
posts.first.comments.pluck(:id)
should also raise anActiveRecord::StrictLoadingViolationError
just as theto_a
does.Actual behavior
Calling
posts.first.comments.pluck(:id)
does not raise an error and calls N+1 queries insteadSystem configuration
Rails version:
7.1.3.2
Ruby version:
3.2.2
There is a similar issue raised #49520 and I also referenced the provided fix in #50389 in the POC and the issue still remains. Hence raising this issue as separate.
The text was updated successfully, but these errors were encountered: