In Rails, we can mark certain attributes of a model as readonly using the attr_readonly
method. Once an attribute is marked as readonly, it cannot be changed after the record is created.
Before
If we try to assign a new value to a readonly attribute, the assignment operation would appear to succeed, but the change would not be persisted to the database.
After
Rails 7.1 now raises an ActiveRecord::ReadonlyAttributeError on assignment to readonly attributes
This explicit ActiveRecord::ReadonlyAttributeError
error makes it clear that we are trying to modify an attribute that should not be changed, preventing the silent failure in previous versions that could lead to false positives.
Rails 7.1 introduces a configuration option to control readonly attribute behavior and it is enabled by default with load_defaults 7.1
.
To retain the old behavior we can simply disable it.