# ANN: attr\_immutable plugin

**URL:** <https://www.ruby-forum.com/t/ann-attr-immutable-plugin/48988>\
**Category:** Rails\
**Created:** [November 28, 2005, 9:52am UTC](https://www.ruby-forum.com/t/ann-attr-immutable-plugin/48988 "2005-11-28T09:52:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 28, 2005, 9:52am UTC](https://www.ruby-forum.com/t/ann-attr-immutable-plugin/48988/1 "2005-11-28T09:52:58Z")

</div>

Hiho,

Thought this might of some use to someone out there.

- John

* * *

* * *

DESCRIPTION:

attr\_immutable is simple plugin for Rails 0.14.3+ which will cause  
attributes  
flagged as immutable to be just that: immutable. The attributes  
flagged will  
be rendered frozen on read, and throw an  
ImmutableAttributeAssignmentError on  
assignment only _after_ the record has been persisted (save/  
create) or loaded  
(find).

Comments and suggestions welcome.

* * *

* * *

INSTALLATION:

Download and extract the following into your vendor/plugins  
directory.

```
 http://www.psynix.com/rails/attr_immutable/attr_immutable.tar.gz
 http://www.psynix.com/rails/attr_immutable/attr_immutable.tar.bz2

```

* * *

* * *

USAGE:

class User \< ActiveRecord  
attr\_immutable :username # Make username immutable after  
save/create  
end

me = User.new  
me.username = ‘testuser’ =\> ‘testuser’  
me.password = ‘testpassword’ =\> ‘testpassword’  
me.save =\> true  
me.reload  
me.username.frozen? =\> true  
me[:username].frozen? =\> true  
me.attributes[‘username’].frozen? =\> true  
me.username = ‘anotherusername’ =\>  
Plasma::ImmutableAttributeAssignmentError thrown  
me[:username] = ‘anotherusername’ =\>  
Plasma::ImmutableAttributeAssignmentError thrown

This plugin was developed using Rails 0.14.3. It may work on previous  
versions. Use at your own risk.

–  
John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 28, 2005, 11:05am UTC](https://www.ruby-forum.com/t/ann-attr-immutable-plugin/48988/2 "2005-11-28T11:05:45Z")

</div>

Don’t wanna step on your toes, but one could as well do

class User \< ActiveRecord  
attr\_protected :name  
end

Then you as an admin would still have the possibilibty to change the  
username through proper controllers.

Your solution might have it’s place though - it’s certainly a cleaner  
solution if one never ever wants to change an attribute  
(Just wrote this in case you didn’t know about attr\_protected)

Cheers 🙂  
Rob

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [November 28, 2005, 1:43pm UTC](https://www.ruby-forum.com/t/ann-attr-immutable-plugin/48988/3 "2005-11-28T13:43:51Z")

</div>

Hi,

I’m fully aware of attr\_protected … it does however serve a  
different purpose ie. that of disabling mass assignment of specified  
attributes.

Thanks for the comments anyway 🙂

- John

On 28/11/2005, at 8:03 PM, Robert wrote:

> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> –  
> John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
