# Wonky Validation, attr\_accessor

**URL:** <https://www.ruby-forum.com/t/wonky-validation-attr-accessor/161272>\
**Category:** Rails\
**Created:** [February 24, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/wonky-validation-attr-accessor/161272 "2009-02-24T23:29:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@Viral](https://www.ruby-forum.com/u/Viral)\
**Post date:** [February 24, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/wonky-validation-attr-accessor/161272/1 "2009-02-24T23:29:17Z")

</div>

# I have a somewhat basic question, I have my User Model with the

following attributes:

# 

# 

# User.email

# User.hashed\_password

# User.salt

# 

# Attr Accessors

# 

# User.password\_confirmation

# User.email\_confirmation

# 

class User \< ActiveRecord::Base

validates\_length\_of :password, :within =\> 5…40  
validates\_uniqueness\_of :email

validates\_presence\_of :email, :email\_confirmation, :password,  
:password\_confirmation

validates\_confirmation\_of :password  
validates\_confirmation\_of :email

validates\_format\_of :email, :with =\> /^([^@\s]+)@((?:[-a-z0-9]+.)+  
[a-z]{2,})$/i, :message =\> “email address didn’t validate, example  
removed\_email\_address@domain.invalid”

attr\_protected :id, :salt

attr\_accessor :password, :password\_confirmation, :email\_confirmation

end

# 

# In my users\_controller, I have 2 action, change\_email and

change\_password.

# 

# What is the best way to update for just a password change OR email

change (different actions). The reason

# I’m asking this is that whenever I try and save just password and

password\_confirmation, rails returns

# errors for email not being present. (it’s not on the form in

change\_password action)

# 

# My problem seems to be this, all my users validators are firing even

though I’m only editting a select

# attributes from my model.

# 

# What’s the best solution guys?

# 

# Thanks,

# - Viral

---

<div class="post-metadata">

**Author:** ![Viral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@Viral](https://www.ruby-forum.com/u/Viral)\
**Post date:** [February 25, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/wonky-validation-attr-accessor/161272/2 "2009-02-25T17:18:44Z")

</div>

Here’s how it should look like:

class User \< ActiveRecord::Base

validates\_length\_of :password, :within =\> 5…40  
validates\_uniqueness\_of :email

validates\_presence\_of :email, :password

validates\_confirmation\_of :password, :if =\> :password\_changed?  
#require confirmation only if it has changed  
validates\_confirmation\_of :email, :if =\> :email\_changed?

validates\_format\_of :email, :with =\>  
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :message =\> “email  
address didn’t validate, example removed\_email\_address@domain.invalid”

attr\_protected :id, :salt

attr\_accessor :password, :password\_confirmation, :email\_confirmation

end

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![Viral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/4bbf92/32.png) [@Viral](https://www.ruby-forum.com/u/Viral)\
**Post date:** [February 27, 2009, 4:47am UTC](https://www.ruby-forum.com/t/wonky-validation-attr-accessor/161272/3 "2009-02-27T04:47:20Z")

</div>

Yup, that’s basically what I did when I gutted everything to use  
AuthenicatedSystem (acts\_as\_authenicated)

Thanks ;D

On Feb 25, 11:17 am, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
