Last active
August 29, 2015 14:16
-
-
Save tbreier/c167ddc63902ea7a17af to your computer and use it in GitHub Desktop.
ActiveRecord 4.2.0 bug with ID parameters in nested FROM subqueries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.2.0' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# 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| | |
end | |
create_table :comments, force: true do |t| | |
t.integer :post_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
post = Post.create! | |
post_id = post.id | |
post.comments << Comment.create! | |
simple_object = Comment.where(post: post).to_sql | |
simple_id = Comment.where(post_id: post_id).to_sql | |
one_level_object = Comment.from(Comment.where(post: post), :comments).to_sql | |
one_level_id = Comment.from(Comment.where(post_id: post_id), :comments).to_sql | |
two_levels_object = Comment.from(Comment.from(Comment.where(post: post), :comments), :comments).to_sql | |
# this one is broken (post_id doesn't get properly inserted?) | |
two_levels_id = Comment.from(Comment.from(Comment.where(post_id: post_id), :comments), :comments).to_sql | |
[:simple_object, :simple_id, :one_level_object, :one_level_id, :two_levels_object, :two_levels_id].each do |test_case| | |
puts "---------------" | |
puts "#{test_case}:" | |
puts eval(test_case.to_s) | |
end | |
assert_equal simple_object, simple_id, 'Simple Query' | |
assert_equal one_level_object, one_level_id, "One level of 'from'" | |
assert_equal two_levels_object, two_levels_id, "Two levels of 'from'" | |
end | |
end | |
# | |
# --------------- | |
# simple_object: | |
# SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 | |
# --------------- | |
# simple_id: | |
# SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 | |
# --------------- | |
# one_level_object: | |
# SELECT "comments".* FROM (SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1) comments | |
# --------------- | |
# one_level_id: | |
# SELECT "comments".* FROM (SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1) comments | |
# --------------- | |
# two_levels_object: | |
# SELECT "comments".* FROM (SELECT "comments".* FROM (SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1) comments) comments | |
# --------------- | |
# two_levels_id: | |
# SELECT "comments".* FROM (SELECT "comments".* FROM (SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ) comments) comments | |
# F | |
# | |
# Finished in 0.063785s, 15.6777 runs/s, 47.0330 assertions/s. | |
# | |
# 1) Failure: | |
# BugTest#test_association_stuff [active_record_gem.rb:56]: | |
# Two levels of 'from'. | |
# --- expected | |
# +++ actual | |
# @@ -1 +1 @@ | |
# -"SELECT \"comments\".* FROM (SELECT \"comments\".* FROM (SELECT \"comments\".* FROM \"comments\" WHERE \"comments\".\"post_id\" = 1) comments) comments" | |
# +"SELECT \"comments\".* FROM (SELECT \"comments\".* FROM (SELECT \"comments\".* FROM \"comments\" WHERE \"comments\".\"post_id\" = ) comments) comments" | |
# | |
# | |
# 1 runs, 3 assertions, 1 failures, 0 errors, 0 skips | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment