Actions
Refactor #16792
closedDon't modify AR record attributes in-place during normalisation
Description
Rails 5 now prefers to call #freeze on any string passed in as an attribute during validation, but this means attributes must not be edited in-place with gsub! and similar methods. Some of the pre-validation methods used to normalise hostnames, OS names etc will attempt to modify attributes in-place with gsub!, e.g.
HostsControllerTest::Fog.mock!#test_0001_#process_hostgroup changes compute attributes:
RuntimeError: can't modify frozen String
app/models/nic/interface.rb:101:in `gsub!'
app/models/nic/interface.rb:101:in `normalize_name'
app/models/concerns/orchestration.rb:77:in `valid?'
app/models/concerns/orchestration.rb:77:in `valid?'
app/models/concerns/foreman/sti.rb:29:in `save_with_type'
app/models/host.rb:15:in `method_missing'
test/functional/hosts_controller_test.rb:1271:in `initialize_host'
Prefer instead to retrieve and set the attribute through the regular attribute setter API (resource.foo = resource.foo.gsub(..)
).
https://github.com/rails/rails/issues/24185 and related tickets have more info.
Actions