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"git_source(:github){ |repo| "https://github.com/#{repo}.git"}gem"rails",github: "rails/rails",branch: "main"# If you want to test against edge Rails replace the previous line with this:# gem "rails", github: "rails/rails", branch: "main"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|
t.integer:first_comment_idt.integer:last_comment_idendcreate_table:comments,force: truedo |t|
t.integer:post_idendendclassPost < ActiveRecord::Basehas_many:commentsbelongs_to:first_comment,class_name: "Comment"belongs_to:last_comment,class_name: "Comment"scope:select_another_column,->{select("*").select("TIME('now') as now")}endclassComment < ActiveRecord::Basebelongs_to:postendclassBugTest < Minitest::Testdeftest_association_stuffpost=Post.create!post.first_comment=Comment.create!post.last_comment=Comment.create!post.save!post=Post.find(post.id)post_with_eager_loads=Post.eager_load(:first_comment,:last_comment).select_another_column.find(post.id)assert_equalpost.id,post_with_eager_loads.idendend
Expected behavior
Expect that IDs are consistent when using .eager_load
Actual behavior
ID gets set to one of the other tables joined in
System configuration
Rails version: main
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered:
@PhilCoggins When using select("*") in a joins query, SQL will select all columns from all tables involved in the join. This can lead to unexpected results, especially in cases where tables share column names (the id column, in this case). The ambiguity of identical column names can cause data from one table to overwrite data from another.
A more precise way to handle this situation is to specify the table name in the query, using select("posts.*").
Change select("*").select("TIME('now') as now") to select("posts.*, TIME('now') as now") should solve the issue.
Steps to reproduce
Expected behavior
Expect that IDs are consistent when using .eager_load
Actual behavior
ID gets set to one of the other tables joined in
System configuration
Rails version: main
Ruby version: 3.3.0
The text was updated successfully, but these errors were encountered: