# Confusing behavior with attr\_readonly

**URL:** <https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835>\
**Category:** rubyonrails-core\
**Created:** [February 26, 2009, 6:36pm UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835 "2009-02-26T18:36:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trevor\_Turk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/trevor_turk/32/11634_2.png) [@Trevor\_Turk](https://discuss.rubyonrails.org/u/Trevor_Turk)\
**Post date:** [February 26, 2009, 6:36pm UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/1 "2009-02-26T18:36:53Z")

</div>

I've done a little write-up of some confusing behavior around attr\_readonly.

The upshot is that I think we should make attr\_readonly act more like attr\_protected. Failing that, I think we should consider change the behavior of update\_attribute to make things a bit less confusing.

Here's the gist:

> <https://gist.github.com/trevorturk/70955>

Thanks, - Trevor

---

<div class="post-metadata">

**Author:** ![M11](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@M11](https://discuss.rubyonrails.org/u/M11)\
**Post date:** [February 27, 2009, 6:48pm UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/2 "2009-02-27T18:48:26Z")

</div>

Hi -

To your options at the bottom, I might add:

d) silently ignore update\_attribute, in the same way that attr\_protected silently ignores mass assignment. (to deal with the issue of the obj in memory appearing to be updated)

this seems to be most in keeping with the doc:

"Attributes listed as readonly can be set for a new record, but will be ignored in database updates afterwards."

So, allowing update\_attribute to work on this might be somewhat surprising for people who are expecting to the behavior in the current doc. On the other hand, update\_attribute not working might be more surprising...

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [March 2, 2009, 5:23am UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/3 "2009-03-02T05:23:59Z")

</div>

> So, allowing update\_attribute to work on this might be somewhat surprising for people who are expecting to the behavior in the current doc. On the other hand, update\_attribute not working might be more surprising...

I think I like option a. It's consistent with attr\_protected and allows people who 'know what they're doing' to override that read only setting if they desperately need to, and avoids the horrible case of an object \*appearing\* updated but not being persisted with the updates applied.

---

<div class="post-metadata">

**Author:** ![Trevor\_Turk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/trevor_turk/32/11634_2.png) [@Trevor\_Turk](https://discuss.rubyonrails.org/u/Trevor_Turk)\
**Post date:** [March 2, 2009, 5:32am UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/4 "2009-03-02T05:32:29Z")

</div>

Thanks for the replies, guys. I think I like option a, too. I expected update\_attribute to work, and I can't imagine that anyone is counting on it not working.

If I don't hear any objections I'll try to work up a patch with doc fixes tomorrow and create a Lighthouse ticket.

Thanks, - Trevor

---

<div class="post-metadata">

**Author:** ![Trevor\_Turk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/trevor_turk/32/11634_2.png) [@Trevor\_Turk](https://discuss.rubyonrails.org/u/Trevor_Turk)\
**Post date:** [March 2, 2009, 7:08pm UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/5 "2009-03-02T19:08:13Z")

</div>

I've done some research and added a new file to the original gist:

[http://gist.github.com/70955](http://gist.github.com/70955)

You're probably best off viewing it here:

[http://gist.github.com/raw/70955/9ccc058386921fefcc9f9d3e6a9ca0b658619e63/gistfile2.txt](http://gist.github.com/raw/70955/9ccc058386921fefcc9f9d3e6a9ca0b658619e63/gistfile2.txt)

The upshot is that I feel changing the behavior of update\_attribute may have unexpected consequences, and I believe the short-term solution may be to simply raise an exception if update\_attribute is given a readonly attribute.

This may be the easiest solution to implement, but it wouldn't have the 100% desired effect (e.g. allowing update\_attribute to work on a readonly attribute). It would be low impact, though, and at least it would notify the user that the action they're trying to take won't work.

That way, at least the behavior would be clear. The fact that update\_attribute appears to work but doesn't actually work is the "bug" in my mind. I'd prefer to make it actually work, but the options I explored may end up being more problematic than are justified by the issue at hand.

Thanks, - Trevor

---

<div class="post-metadata">

**Author:** ![Michael\_Koziarski1](https://avatars.discourse-cdn.com/v4/letter/m/aca169/32.png) [@Michael\_Koziarski1](https://discuss.rubyonrails.org/u/Michael_Koziarski1)\
**Post date:** [March 2, 2009, 9:36pm UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/6 "2009-03-02T21:36:05Z")

</div>

> That way, at least the behavior would be clear. The fact that update\_attribute appears to work but doesn't actually work is the "bug" in my mind. I'd prefer to make it actually work, but the options I explored may end up being more problematic than are justified by the issue at hand.

I completely agree with your assesment of the issue, create a new exception subclass along the lines of ReadOnlyAttributeError and make it raise. Just be sure that the exception tells you what the read only attribute was ;).

Thanks for spending the time on this.

---

<div class="post-metadata">

**Author:** ![Trevor\_Turk](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/trevor_turk/32/11634_2.png) [@Trevor\_Turk](https://discuss.rubyonrails.org/u/Trevor_Turk)\
**Post date:** [March 5, 2009, 5:24am UTC](https://discuss.rubyonrails.org/t/confusing-behavior-with-attr-readonly/39835/7 "2009-03-05T05:24:50Z")

</div>

I worked up a patch and created a ticket on Lighthouse here:

[http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2132-confusing-behavior-with-attr\_readonly](http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2132-confusing-behavior-with-attr_readonly)

I also did a little more research and found some other methods that might be similarly confusing. Everything is detailed in the ticket.

Thanks, - Trevor
