Skip to content

Instantly share code, notes, and snippets.

@michaelmwu
Created July 9, 2014 05:16
Show Gist options
  • Save michaelmwu/9238933069e45723b982 to your computer and use it in GitHub Desktop.
Save michaelmwu/9238933069e45723b982 to your computer and use it in GitHub Desktop.
No scope pollution
gem 'activerecord', '~>3.2.1'
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 do |t|
t.integer :status
end
end
class Post < ActiveRecord::Base
scope :status_1, ->{
post = Post.where(status: 2).first
if post.blank?
raise "None found"
end
where("1=1")
}
end
class BugTest < Minitest::Test
def test_association_stuff
Post.create!(status: 1)
Post.create!(status: 2)
post_1 = Post.status_1.first
assert_equal 1, post_1.status
post_2 = Post.where(status: 2).first
assert_equal 2, post_2.status
Post.where(status: 1).status_1.first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment