From 6e1f789e68139237fec379d177f270b217449c9a Mon Sep 17 00:00:00 2001 From: Dmitry Mineev Date: Tue, 12 Mar 2024 13:11:01 +0100 Subject: [PATCH] After [this commit](https://github.com/rails/rails/blob/c9c293e19464c9ce37316f2d917ac123d7b654e6/activerecord/lib/active_record/model_schema.rb#L395) `ActiveRecord.column_defaults` returns frozen hash and this behaviour brakes the state machine `initialize_state` method. This PR fixes the issue --- CHANGELOG.md | 1 + lib/state_machine/machine.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5a224a3..c8d38aa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Raise InvalidContext error when the current state does not define a state-driven behavior * Fix target state being indeterminate for transitions that use blacklists * Allow super to be called within state-driven behaviors +* Fix issue with Rails 6.1+ where ActiveRecord.column_defaults returns `frozen` hash ## 1.2.0 / 2013-03-30 diff --git a/lib/state_machine/machine.rb b/lib/state_machine/machine.rb index 6aa6eae0..d9ed69b2 100644 --- a/lib/state_machine/machine.rb +++ b/lib/state_machine/machine.rb @@ -701,7 +701,7 @@ def initialize_state(object, options = {}) if state && (options[:force] || initialize_state?(object)) value = state.value - if hash = options[:to] + if hash = options[:to].dup hash[attribute.to_s] = value else write(object, :state, value)