Skip to content

Instantly share code, notes, and snippets.

@al2o3cr
Created September 20, 2015 17:54
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/86ec767b9140729f86fa to your computer and use it in GitHub Desktop.
Save al2o3cr/86ec767b9140729f86fa to your computer and use it in GitHub Desktop.
inverse_of regression in 4.2
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: tests pass on 4.1.x, fail on 4.2
gem 'activerecord', '4.2.4'
gem 'sqlite3'
end
# 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, through: :owned_customer, source: :programs, inverse_of: :owner, class_name: 'Program'
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
has_one :owner, through: :customer, inverse_of: :owned_programs#, class_name: 'User'
end
class BugTest < Minitest::Test
def test_inverses
assert Program.reflect_on_association(:owner).inverse_of.present?
end
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.customer.present?
assert new_program.owner.present?
new_program.name = 'foo'
assert new_program.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment