Skip to content

Instantly share code, notes, and snippets.

@ksylvest
Created March 16, 2017 00:18
Show Gist options
  • Save ksylvest/92d32bb02101909b45adfd456a4ed29a to your computer and use it in GitHub Desktop.
Save ksylvest/92d32bb02101909b45adfd456a4ed29a 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"
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.0.2"
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 :posts, force: true do |t|
end
create_table :tags, force: true do |t|
t.datetime :deleted_at
end
create_table :post_tags, force: true do |t|
t.references :post, null: false
t.references :tag, null: false
end
end
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, -> { active }, through: :post_tags
end
class Tag < ActiveRecord::Base
has_many :post_tags
has_many :posts, through: :post_tags
scope :active, -> { where(deleted_at: nil) }
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
tag = Tag.create!
PostTag.create!(tag: tag, post: post)
# Passes:
begin
Post.all.map{ |post| post.tags }
rescue ActiveRecord::StatementInvalid => error
flunk("raised '#{error.inspect}'")
end
# Fails:
begin
Post.preload(:tags).map{ |post| post.tags }
rescue ActiveRecord::StatementInvalid => error
flunk("raised '#{error.inspect}'")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment