Skip to content

Instantly share code, notes, and snippets.

@al2o3cr
Last active September 3, 2015 00:44
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 al2o3cr/3b694da67151e47912a5 to your computer and use it in GitHub Desktop.
Save al2o3cr/3b694da67151e47912a5 to your computer and use it in GitHub Desktop.
Issue found investigating rails/rails #21466
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'
# NOTE: test passes on 4.1.13, fails against 4.2.0+
# gem 'activerecord', '4.1.13'
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|
t.integer :customer_id
t.string :name
t.string :role
end
create_table :customers, force: true do |t|
t.string :name
end
create_table :programs, force: true do |t|
t.integer :customer_id
t.string :name
end
end
class User < ActiveRecord::Base
belongs_to :owned_customer, class_name: "Customer", inverse_of: :owner, foreign_key: :customer_id
has_many :owned_programs, inverse_of: :owner, through: :owned_customer, source: :programs
end
class Customer < ActiveRecord::Base
has_one :owner, -> { where(:role => 'owner') }, class_name: "User", inverse_of: :owned_customer, dependent: :destroy
has_many :programs, inverse_of: :customer, dependent: :restrict_with_exception
end
class Program < ActiveRecord::Base
belongs_to :customer, inverse_of: :programs
# Bug is here. Removing inverse_of makes the test pass
# With inverse_of on AR 4.2.3, ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]:
#
# 1) Error:
# BugTest#test_association_stuff:
# NameError: uninitialized constant Program::Owner
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/inheritance.rb:158:in `compute_type'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:271:in `compute_class'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:267:in `klass'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:393:in `inverse_of'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:336:in `check_validity_of_inverse!'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:877:in `check_validity_of_inverse!'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/reflection.rb:855:in `check_validity!'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/associations/association.rb:25:in `initialize'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/associations.rb:162:in `new'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/associations.rb:162:in `association'
# /opt/rubies/2.1.2/lib/ruby/gems/2.1.0/gems/activerecord-4.2.3/lib/active_record/associations/builder/association.rb:115:in `owner'
# test_associations.rb:88:in `test_association_stuff'
has_one :owner, through: :customer, inverse_of: :owned_programs
end
class BugTest < Minitest::Test
def test_association_stuff
customer = Customer.create!(name: 'Nobody')
program = Program.create!(name: 'Ed Norton Utilities', customer: customer)
user = User.create!(name: 'Gorbypuff', role: 'owner', owned_customer: customer)
new_program = Program.last
assert new_program.owner.present?
assert new_program.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment