Skip to content

Instantly share code, notes, and snippets.

@mleszcz
Created June 27, 2015 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mleszcz/6df56f688dc9daae04b4 to your computer and use it in GitHub Desktop.
Save mleszcz/6df56f688dc9daae04b4 to your computer and use it in GitHub Desktop.
rails/rails#20718 reproduction script
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', '4.2.3'
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 :users, force: true do |t|
end
create_table :subscriptions, force: true do |t|
t.integer :user_id
t.integer :package_id
end
create_table :packages, force: true do |t|
end
end
class User < ActiveRecord::Base
has_one :subscription, -> { where(package_id: Package.permanent) }
has_one :package, through: :subscription
end
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :package
end
class Package < ActiveRecord::Base
has_many :subscriptions
scope :permanent, -> { where(id: 1) }
end
class BugTest < Minitest::Test
def test_association_stuff
user = User.create!
package = Package.create!
subscription = Subscription.create!(user_id: user.id, package_id: package.id)
user.reload
assert_equal 1, User.count
assert_equal 1, Package.count
assert_equal 1, Subscription.count
assert_equal 1, Package.permanent.count
assert_equal user.subscription, subscription
assert_equal user.package, package
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment