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"gem"sqlite3","~> 1.4"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:categoriesdo |t|
t.integer:parent_category_idendendclassCategory < ActiveRecord::Basedefself.parentsCategory.where(id: select(:parent_category_id))endendclassBugTest < Minitest::Testdeftest_association_stuffparent=Category.create!(parent_category_id: nil)category=Category.create!(parent_category_id: parent.id)assert_equal([parent.id],Category.where(id: Category.where(id: category.id).select(:parent_category_id)).pluck(:id))assert_equal([parent.id],Category.where(id: category.id).parents.pluck(:id))endend
Expected behavior
These two assertions should be equivalent. The only difference between them is that, in the second case, some of the logic is contained in a class method.
Actual behavior
The second query is generated incorrectly. The part that filters for a specific id is put into the top-level query when it shouldn't have been.
System configuration
Rails version: 7.1.3.3 Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered:
One of the differences between the first and second assertions is that the second assertion chains the parents method to the Category.where(id: category.id) query. In other words, it calls the parents method on the result of the Category.where(id: category.id) query. Since the parent record is not included in the result of the Category.where(id: category.id) query, calling the parents method on this result will not return the parent record.
To prove this, you can call Category.parents.pluck(:id), which should pass the test. In this case, all Category records will be available for the parent chain method.
Hi @danielwaterworth , thanks for the report. The two queries are not the same. If you were to expand the self.parents method on the base scope in the second query, it would look something like this:
Steps to reproduce
Expected behavior
These two assertions should be equivalent. The only difference between them is that, in the second case, some of the logic is contained in a class method.
Actual behavior
The second query is generated incorrectly. The part that filters for a specific id is put into the top-level query when it shouldn't have been.
System configuration
Rails version: 7.1.3.3
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: