Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created April 9, 2016 00:15
Show Gist options
  • Save jturkel/f5c0d53c3af652d7922eda686deb1553 to your computer and use it in GitHub Desktop.
Save jturkel/f5c0d53c3af652d7922eda686deb1553 to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'activerecord', '4.2.4'
gem 'goldiloader'
gem 'sqlite3'
end
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 :blogs, force: true
create_table :posts, force: true do |t|
t.integer :blog_id
t.boolean :active
end
end
class Blog < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :blog
end
class BugTest < Minitest::Test
def test_select
blog = Blog.create!
active_posts = 2.times.map { blog.posts.create!(active: true) }
inactive_posts = 2.times.map { blog.posts.create!(active: false) }
found_active_posts = Blog.find(blog.id).posts.select { |post| post.active? }
assert_equal(active_posts.sort_by(&:id), found_active_posts.sort_by(&:id))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment