Skip to content

Instantly share code, notes, and snippets.

@AmShaegar13
Last active August 29, 2015 14:21
Show Gist options
  • Save AmShaegar13/bb7dc2352588226ec0f4 to your computer and use it in GitHub Desktop.
Save AmShaegar13/bb7dc2352588226ec0f4 to your computer and use it in GitHub Desktop.
Count results with custom `from` breaks
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
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|
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_arel_sql_literals_in_from_with_includes_where_and_count
post = Post.create!
post.comments << Comment.create!
sql = "(SELECT * FROM #{Comment.table_name}) #{Comment.table_name}"
assert_equal 1, Comment.from(sql).joins(:post).includes(:post).where(post_id: post.id).count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment