Rails:Runtimeerror - Can't Modify Frozen Array When Running Rspec in Rails

RuntimeError: can't modify frozen Array (Rollbar, Rails 5.1 upgrade)

In most cases, that error is a red herring for something else.

When encountering it, don't get overwhelmed with the recurrent can't modify frozen Array error messages, and instead check the very first error that appears when running a spec.

For example:

Failure/Error: validate :uniqueness, if: 'should_be_unique?'

ArgumentError: Passing string to be evaluated in :if and :unless
conditional options is not supported. Pass a symbol for an instance
method, or a lambda, proc or block, instead.

Rails test: RuntimeError: can't modify frozen String

I figured it out, turns out it was related to the Algolia tags section in the model. I upgraded algolia-rails package to a newer version and also changed:

["site_#{site.id}", status]

to:

["site_#{site.id}", status.dup]

Rails: Frozen Error when trying to run rails application

Finally, I found the reason. I will explain here the way which I found the solution for the issue so that it may help others who encounter the same error.

I saw the FrozenError class documentation and found that this was introduced in ruby 2.5.x and later. This is the document
FrozenError Doc . I just first upgraded ruby version to latest 2.6.x but this didn't solve the issue.

Fix:

  • Downgraded the ruby version below 2.5.x to 2.4.x.
  • Now in 2.4.x, this FrozenError class does not exist.
  • I could now see the real error that caused the issue. It was a
    SystemStackError.
  • It was caused due a gem in project (wicked_pdf).
  • Removing the gem solved my issue.


Related Topics



Leave a reply



Submit