Skip to content

Instantly share code, notes, and snippets.

@eeeschwartz
Last active December 13, 2015 22:49
Show Gist options
  • Save eeeschwartz/4987741 to your computer and use it in GitHub Desktop.
Save eeeschwartz/4987741 to your computer and use it in GitHub Desktop.
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
@eeeschwartz
Copy link
Author

Updated to match original repro correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment