Skip to content

Instantly share code, notes, and snippets.

@senny
Created September 3, 2013 15:07
Show Gist options
  • Save senny/6425163 to your computer and use it in GitHub Desktop.
Save senny/6425163 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
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 do |t|
t.string :title
end
end
class Post < ActiveRecord::Base
def self.total_post_count
Post.count
end
end
class BugTest < Minitest::Test
def test_scope_leak
Post.create! title: "one"
Post.create! title: "two"
Post.create! title: "three"
assert_equal 3, Post.total_post_count
assert_equal 3, Post.where(title: "one").total_post_count
end
end
@senny
Copy link
Author

senny commented Sep 3, 2013

Fails with:

  1) Failure:
BugTest#test_scope_leak [bug.rb:29]:
Expected: 3
  Actual: 1

1 runs, 2 assertions, 1 failures, 0 errors, 0 skips

the relevant SQL is:

D, [2013-09-03T17:36:00.071743 #33892] DEBUG -- :    (0.1ms)  SELECT COUNT(*) FROM "posts"
D, [2013-09-03T17:36:00.083370 #33892] DEBUG -- :    (0.1ms)  SELECT COUNT(*) FROM "posts"  WHERE "posts"."title" = 'one'

@senny
Copy link
Author

senny commented Feb 10, 2014

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