Attr_readonly confusing behavior

I created this issue `attr_readonly`: surprising inconsistency between what's saved and what's in memory · Issue #41520 · rails/rails · GitHub the other day. Before I start writing any code for it, I’d love to get some wider feedback on it.

The gist of the issue is that if you have an attr_readonly attribute on a model, changes you make to it by assigning the attribute directly or calling update and friends will be reflected on the in-memory object, but will be ignored when saving. There’s no indication of this, no validation errors or warnings logged.

My proposal is to add a configurable way of being notified about this, inspired by the Strict Loading feature. By default, writing to a readonly attribute for a non-new record would raise an error in development and test, and log a warning in production.

I’d love to get feedback or opinions on this!

ps. funnily enough, a similar issue was raised 10 years ago: Confusing behavior with attr_readonly

2 Likes

I am adding docs for this: Add docs for `attr_readonly` by ghiculescu · Pull Request #42762 · rails/rails · GitHub

There’s a PR to fix the issue, but there’s concerns about performance: Make assigning a readonly attribute a no-op by ghiculescu · Pull Request #42705 · rails/rails · GitHub

How many people use the readonly attributes feature?