Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daniel-rikowski/a4d868fe6de6647d2a6a to your computer and use it in GitHub Desktop.
Save daniel-rikowski/a4d868fe6de6647d2a6a to your computer and use it in GitHub Desktop.
Minimal sample to produce "Can't modify frozen hash" when rolling back transaction
gem 'activerecord', '4.2.0' # No error with 4.1.8
require 'active_record'
require 'minitest/autorun'
require 'logger'
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :stylesheets do |t|
t.string :name
end
create_table :settings do |t|
t.integer :parent_id
t.integer :stylesheet_id
t.string :name
end
end
class Stylesheet < ActiveRecord::Base
has_many :settings, -> { where(parent_id: nil) }, dependent: :destroy # No error without `dependent: :destroy`
end
class Setting < ActiveRecord::Base
belongs_to :stylesheet
# No error if these (unused) associations are removed
belongs_to :parent, class_name: 'Setting', inverse_of: :children
has_many :children, class_name: 'Setting', inverse_of: :parent
end
class BugTest < Minitest::Test
def test_double_destroy_in_transaction
Setting.transaction do
stylesheet = Stylesheet.create!(name: 'sheet1')
setting = stylesheet.settings.create!(name: 'setting1')
# No error if only one destroy is called.
setting.destroy
stylesheet.destroy
raise ActiveRecord::Rollback # Results in "Can't modify frozen hash" error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment