# Update\_attribute with Validation?

**URL:** <https://www.ruby-forum.com/t/update-attribute-with-validation/54239>\
**Category:** Rails\
**Created:** [February 27, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239 "2006-02-27T14:55:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [February 27, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239/1 "2006-02-27T14:55:39Z")

</div>

Does anyone know of a way to update individual attributes with  
validation? update\_attribute disables the validation checks in the  
record model, so I’m wondering if there’s a workaround for this.

In my application, I want to allow the user to update some or all the  
attributes without having to enter :password and :password\_confirmation,  
both of which are subject to validation.

Boram

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [February 27, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239/2 "2006-02-27T15:52:03Z")

</div>

On 2/27/06, Boram Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does anyone know of a way to update individual attributes with  
> validation? update\_attribute disables the validation checks in the  
> record model, so I’m wondering if there’s a workaround for this.
> 
> In my application, I want to allow the user to update some or all the  
> attributes without having to enter :password and :password\_confirmation,  
> both of which are subject to validation.
> 
> Boram

You can disable validations with the :if option.

class User \< AR::Base  
attr\_accessor :skip\_password  
validates\_confirmation\_of :password, :if =\> :password\_required?

def password\_required?  
@skip\_password != true  
end  
end

@user.skip\_password = true  
@user.update\_attributes(…)

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [February 28, 2006, 1:15am UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239/3 "2006-02-28T01:15:31Z")

</div>

Boram Y. wrote:

> Does anyone know of a way to update individual attributes with  
> validation? update\_attribute disables the validation checks in the  
> record model, so I’m wondering if there’s a workaround for this.
> 
> In my application, I want to allow the user to update some or all the  
> attributes without having to enter :password and :password\_confirmation,  
> both of which are subject to validation.

update\_attributes (note the plural) call validation before updating, so  
why not use that?

–  
Lee

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [February 28, 2006, 10:25am UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239/4 "2006-02-28T10:25:08Z")

</div>

I should’ve tested out my theory before posting that previous post.

I added the following to my user model, which fixed my problem:

def before\_update  
self.hashed\_password = User.hash\_password(self.password)  
end

Thanks again for your help, Rick.

---

<div class="post-metadata">

**Author:** ![boram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/7993a0/32.png) [@boram](https://www.ruby-forum.com/u/boram)\
**Post date:** [February 28, 2006, 10:13am UTC](https://www.ruby-forum.com/t/update-attribute-with-validation/54239/5 "2006-02-28T10:13:10Z")

</div>

Because there are times when the user will not want to update their  
password, and password and password\_confirmation both have  
validates\_presence\_of and validates\_confirmation\_of applied to them.

Rick, thanks very, very much for responding. The solution you gave works  
for the case when I want to bypass password and password confirmation,  
but does not for the case when I want to update the password. I think  
the problem probably has to do with the fact that I’m using a hashed  
password in my user model. I’ll post here if and when I find a work  
around (of course, if anyone has already been through this issue, feel  
free to enlighten my feeble mind).

Thanks!

Lee O’Mara wrote:

> Boram Y. wrote:
> 
> > Does anyone know of a way to update individual attributes with  
> > validation? update\_attribute disables the validation checks in the  
> > record model, so I’m wondering if there’s a workaround for this.
> > 
> > In my application, I want to allow the user to update some or all the  
> > attributes without having to enter :password and :password\_confirmation,  
> > both of which are subject to validation.
> 
> update\_attributes (note the plural) call validation before updating, so  
> why not use that?
> 
> –  
> Lee
