Skip to content

FrozenError when calling .new on STI model with state_machine in Rails 6.1 #363

Open

Description

@kreintjes

Calling .new on an ActiveRecord Single Table Inheritance model with a state_machine defined results in a "FrozenError: can't modify frozen Hash" since Rails 6.1.

Example, suppose we have the following model:

class Car < ApplicationRecord
  state_machine do
  end
end

which has a state and type column (the latter enabling STI) and some subclasses extending from Car. We would expect Car.new to return a new Car instance, but instead it crashes with "FrozenError: can't modify frozen Hash:".

Rails version: 6.1.4.1
Ruby version: 2.7.3
State machine version: 1.2.0

This did work in Rails 6.0 and before.

Activity

  1. slgriffiths commented on Apr 1, 2022

    @slgriffiths

    I'm also hitting the "can't modify Frozen Hash" on Rails 6.1.4.4.

    My annotate gem actually triggered the error and adding some tracing to the call links it back to state_machine. We also did not have this issue until we updated rails.

  2. t56k commented on May 11, 2022

    @t56k

    Same here from Rails 6.0.5 to 6.1.6. Any advice here?

  3. joshuapinter commented on Aug 24, 2022

    @joshuapinter

    Seeing the same thing here. We make use of column_defaults and you can see that as of Rails 6.1, it was changed to freeze the returned Hash:

    rails/rails@c9c293e

    A number of Hashes were frozen in this commit. Still not sure of a workaround yet but at least we know where it came from and maybe even why.

  4. joshuapinter commented on Aug 24, 2022

    @joshuapinter

    FYI, since this gem hasn't been updated in 10 years, we updated to the state_machines gem (notice the plural "s" there). Specifically, we used the ActiveRecord integration gem called state_machines-activerecord.

    What's super nice is that it's a drop-in replacement.

    We're still conducting testing but it seems to fix our issue on Rails 6.1.

  5. johnnyicon commented on Sep 12, 2023

    @johnnyicon

    Seeing the same thing here. We make use of column_defaults and you can see that as of Rails 6.1, it was changed to freeze the returned Hash:

    rails/rails@c9c293e

    A number of Hashes were frozen in this commit. Still not sure of a workaround yet but at least we know where it came from and maybe even why.

    Just tried this. Swapped in state_machines-activerecord. All my tests that were failing due to this issue are now passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions