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

1 Like