Skip to content

Instantly share code, notes, and snippets.

@chloerei
Created March 31, 2014 14:35
Show Gist options
  • Save chloerei/9893738 to your computer and use it in GitHub Desktop.
Save chloerei/9893738 to your computer and use it in GitHub Desktop.
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 do |t|
t.boolean :trashed
end
create_table :comments do |t|
t.boolean :trashed
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments, -> { unscope(where: :trashed) }
default_scope -> { where(trashed: false) }
end
class Comment < ActiveRecord::Base
belongs_to :post, -> { unscope(where: :trashed) }
default_scope -> { where(trashed: false) }
end
class BugTest < Minitest::Test
def test_includes_has_many_scope
post = Post.create!
comment = Comment.create!(trashed: true)
post.comments << comment
assert_equal comment, Post.first.comments.first
# Pass
# SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? ORDER BY "comments"."id" ASC LIMIT 1 [["post_id", 1]]
assert_equal comment, Post.includes(:comments).first.comments.first
# Failure
# SELECT "comments".* FROM "comments" WHERE "comments"."trashed" = 'f' AND "comments"."post_id" IN (1)
end
def test_includes_belongs_to_scope
post = Post.create!(trashed: true)
comment = Comment.create!
post.comments << comment
assert_equal post, Comment.first.post
# Pass
# SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
assert_equal post, Comment.includes(:post).first.post
# Failure
# SELECT "posts".* FROM "posts" WHERE "posts"."trashed" = 'f' AND "posts"."id" IN (1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment