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

Rails 7.1 Raises Error On Assignment To Readonly Attributes.

submited by
Style Pass
2024-10-15 11:00:03

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.

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.

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.

Leave a Comment