# Protecting fields from being changed

**URL:** <https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169>\
**Category:** Rails\
**Created:** [October 14, 2007, 6:03am UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169 "2007-10-14T06:03:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 6:03am UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/1 "2007-10-14T06:03:41Z")

</div>

Hi all,

I am familiar with attr\_accessible. But lately I have realised that  
it doesnt do exactly what I want it to do.

Once a user is created on the system, I want to prevent the person  
from changing their username.

I need username to be accessible on create… but after that I need it  
to be protected.

Similarly - passwords need to be protected malicious form submits. I  
don’t want peope to be able to change their password in a sneaky way  
when, say, they submit a form to change their address.

Instead, passwords should only be changed after they submit a very  
paticular form, and after they confirm the change through email.

Are there any ideas on how to lock down certain values in a model  
after its initial creation?

thanks!

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 9:46am UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/2 "2007-10-14T09:46:53Z")

</div>

with before\_update

is the idea that i check the submitted fields and throw an exception  
if a protected field is given for an existing record?

is there a clean way to do this already explored?

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 7:14am UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/3 "2007-10-14T07:14:55Z")

</div>

On Oct 13, 9:03 pm, Luzzy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thanks!  
> you could do a before\_update, or try this:

[http://locusfoc.us/2007/2/1/validates\_unchangeable-plugin](http://locusfoc.us/2007/2/1/validates_unchangeable-plugin)

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 9:48am UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/4 "2007-10-14T09:48:03Z")

</div>

On Oct 13, 2007, at 9:03 PM, Luzzy wrote:

> Instead, passwords should only be changed after they submit a very  
> paticular form, and after they confirm the change through email.
> 
> Are there any ideas on how to lock down certain values in a model  
> after its initial creation?

This sounds to me like you’re expecting that an update via  
ActiveRecord will automatically write any data it recieves from a  
form. So, if you have a form that is intended to update a user’s  
name, and they copy the HTML, then add a password field that they  
collected from the original account create form, then submit that  
version, you’re suspicious that the password data actually makes it  
all the way through to the database. Is that what you’re worried about?

Me too. 🙂 I’m new to Rails, and haven’t yet figured out what it has  
to protect against this situation, but in my own framework, my form  
processing had a required attribute which is an explicit list of  
allowed inputs by name. SQL was generated based on which inputs were  
submitted, but no matter what gets sent by POST or GET, and no matter  
even if you handed over the entire params array, the SQL generator  
would only allow those explicitly named inputs to become part of the  
query.

IMO, it’s an essential part of input validation. Accept only what you  
want to accept applies to the actual field count in addition to date/  
format per field. To me the protection you’re looking for doesn’t  
belong as a state of attribute in the model, it belongs at the poing  
of filtering the form to start with. That’s how you achieve fields  
being changed by “a very particular form.”

I would suspect Rails has some mechanism for this input filtering,  
but I haven’t gotten that far yet.

– gw

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/5 "2007-10-14T16:20:16Z")

</div>

On Oct 14, 12:45 am, Luzzy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> with before\_update
> 
> is the idea that i check the submitted fields and throw an exception  
> if a protected field is given for an existing record?
> 
> is there a clean way to do this already explored?

I haven’t tested this, but i think for the fields /columns you want  
write-once, you just return false/nil.

Also, there’s this, and attr\_readonly, but I haven’t figured out how  
to use it:  
[http://constancy.rubyforge.org/](http://constancy.rubyforge.org/)  
[http://dev.rubyonrails.org/ticket/6896](http://dev.rubyonrails.org/ticket/6896)

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/6 "2007-10-14T17:15:15Z")

</div>

Any methods that are declared as protected will not be updated by  
update\_attributes. You must set them explicitly using the attributes  
setter method. This doesn’t prevent them from being changed, but it does  
prevent form data sent to update\_attributes from changing them. Hope  
this helps.

-Bill

---

<div class="post-metadata">

**Author:** ![Luzzy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a698b9/32.png) [@Luzzy](https://www.ruby-forum.com/u/Luzzy)\
**Post date:** [October 14, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/protecting-fields-from-being-changed/117169/7 "2007-10-14T16:29:01Z")

</div>

On Oct 14, 7:19 am, gene tani [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> write-once, you just return false/nil.
> 
> Also, there’s this, and attr\_readonly, but I haven’t figured out how  
> to use it:[http://constancy.rubyforge.org/http://dev.rubyonrails.org/ticket/6896](http://constancy.rubyforge.org/http://dev.rubyonrails.org/ticket/6896)

BTW, this is a pretty common requirement, dozens of blogs. Search for  
“read-only”, “write-once”, “write-protected” “constant” fields/  
columns/ objects/models etc.
