Skip to content

Instantly share code, notes, and snippets.

@diatmpravin
Last active August 29, 2015 13:55
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 diatmpravin/8763881 to your computer and use it in GitHub Desktop.
Save diatmpravin/8763881 to your computer and use it in GitHub Desktop.
AR bug with circular destroy: I am able to replicate this issue for later release including 4.0.0 for has_one relationship. It's fine for earlier release.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '3.2.12'
gem 'arel'
gem 'mysql2'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
#ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.establish_connection(adapter: 'mysql2', host: 'localhost', port: '3306', database: 'testing_test')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
unless ActiveRecord::Base.connection.table_exists? 'physicians'
create_table :physicians do |t|
t.string :name
t.timestamps
end
end
unless ActiveRecord::Base.connection.table_exists? 'patients'
create_table :patients do |t|
t.string :name
t.timestamps
end
end
unless ActiveRecord::Base.connection.table_exists? 'appointments'
create_table :appointments do |t|
t.references :patient
t.references :physician
t.datetime :appointment_date
t.timestamps
end
end
end
class Physician < ActiveRecord::Base
has_many :appointments, dependent: :destroy
has_many :patient, through: :appointment
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient, dependent: :destroy
end
class Patient < ActiveRecord::Base
has_one :appointment, dependent: :destroy
has_one :physician, through: :appointment
end
class BugTest < MiniTest::Unit::TestCase
def test_save_failed
physician = Physician.create!
patient = Patient.create!
appointment = Appointment.create!(physician: physician, patient: patient, appointment_date: Date.today)
#puts physician.appointments.inspect
#puts physician.appointments.size.inspect
#puts physician.appointments(true).empty?.inspect
physician.destroy # here we get a stack level too deep error
assert physician.destroyed?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment