Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Created July 19, 2022 15:32
Show Gist options
  • Save ghiculescu/5bd978b02ed4f1d2072ce6232a8fb120 to your computer and use it in GitHub Desktop.
Save ghiculescu/5bd978b02ed4f1d2072ce6232a8fb120 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3"
end
require "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.define do
create_table :posts, force: true do |t|
t.text :title
end
create_table :comments, force: true do |t|
t.integer :post_id
t.text :message
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_stuff
Comment.create!(post: Post.create!(title: "foo"), message: "bar")
# This works fine. SQL:
# SQL (0.2ms) SELECT "comments"."id" AS t0_r0, "comments"."post_id" AS t0_r1, "comments"."message" AS t0_r2, "posts"."id" AS t1_r0, "posts"."title" AS t1_r1 FROM "comments" LEFT OUTER JOIN "posts" ON "posts"."id" = "comments"."post_id" ORDER BY posts.title LIMIT ? [["LIMIT", 1]]
assert_equal "bar", Comment.includes(:post).order("posts.title").limit(1).take.message
# This raises. SQL:
# Comment Load (0.1ms) SELECT "comments".* FROM "comments" ORDER BY "posts"."title" LIMIT ? [["LIMIT", 1]]
# Error:
# ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.title
assert_equal "bar", Comment.includes(:post).order(Post.arel_table[:title]).limit(1).take.message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment