Last active
June 8, 2017 15:56
-
-
Save palkan/766b8d094c1e1f327c24 to your computer and use it in GitHub Desktop.
rails issue #15468
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'arel', github: 'rails/arel' | |
gem 'sqlite3' | |
gem 'pry-byebug' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
require 'pry-byebug' | |
# 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 :foos do |t| | |
end | |
create_table :quxes do |t| | |
t.references :foo, index: true | |
t.references :ba, polymorphic: true, index: true | |
end | |
create_table :bars do |t| | |
end | |
create_table :bazs do |t| | |
end | |
end | |
class Foo < ActiveRecord::Base | |
has_one :qux, dependent: :destroy, inverse_of: :foo | |
has_one :bar, through: :qux, source: :ba, | |
source_type: 'Bar', inverse_of: :foo | |
has_one :baz, through: :qux, source: :ba, | |
source_type: 'Baz', inverse_of: :foo | |
validates :qux, presence: true | |
validates_associated :qux | |
end | |
class Qux < ActiveRecord::Base | |
belongs_to :foo, inverse_of: :qux | |
belongs_to :ba, polymorphic: true, inverse_of: :qux, dependent: :destroy | |
validates :foo, :ba, presence: true | |
validates_associated :ba | |
end | |
class Bar < ActiveRecord::Base | |
has_one :qux, as: :ba, inverse_of: :ba | |
has_one :foo, through: :qux, inverse_of: :bar | |
validates :qux, :foo, presence: true | |
end | |
class Baz < ActiveRecord::Base | |
has_one :qux, as: :ba, inverse_of: :ba | |
has_one :foo, through: :qux, inverse_of: :baz | |
validates :qux, :foo, presence: true | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
foo = Foo.new | |
foo.build_qux(ba: Bar.new(foo: foo), foo: foo) | |
foo.save! | |
foo.destroy! | |
assert Bar.all.empty? | |
assert Foo.all.empty? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment