Last active
December 13, 2015 22:49
-
-
Save eeeschwartz/4987741 to your computer and use it in GitHub Desktop.
Condensed reproduction of issue described in https://github.com/rails/rails/issues/9303. Also see original repro: https://github.com/tlatim/arel_sum/blob/master/spec/models/book_spec.rb
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
require 'spec_helper' | |
describe Book do | |
it "has same join behavior for logically equivalent subqueries" do | |
author = Author.create | |
book = Book.create!(cost: 100, author: author) | |
2.times { Chapter.create!(book: book) } | |
sub_query1 = Book.arel_table[:author_id].in([author.id]) | |
sub_query2 = Book.arel_table[:author_id].in( | |
Author.arel_table.project(Author.arel_table[:id])) | |
Book.includes([:chapters]).where(sub_query1).sum(:cost).should == | |
Book.includes([:chapters]).where(sub_query2).sum(:cost) | |
# with sub_query1: | |
# SELECT SUM(`books`.`cost`) AS sum_id FROM `books` | |
# WHERE `books`.`author_id` IN (4) | |
# with sub_query2: | |
# SELECT SUM(`books`.`cost`) AS sum_id FROM `books` | |
# LEFT OUTER JOIN `chapters` ON `chapters`.`book_id` = `books`.`id` | |
# WHERE `books`.`author_id` IN (SELECT `authors`.`id` FROM `authors`) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to match original repro correctly