# Help: Validating when Updating a Record

**URL:** <https://www.ruby-forum.com/t/help-validating-when-updating-a-record/71467>\
**Category:** Rails\
**Created:** [October 9, 2006, 6:34am UTC](https://www.ruby-forum.com/t/help-validating-when-updating-a-record/71467 "2006-10-09T06:34:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 9, 2006, 6:34am UTC](https://www.ruby-forum.com/t/help-validating-when-updating-a-record/71467/1 "2006-10-09T06:34:56Z")

</div>

Hi

For my users model, the password field cannot be the same as the  
username, which is checked by the validate method in the model:

```
class User < ActiveRecord::Base
  ...
  def validate
    if password == username
      errors.add(:password, "Cannot be the username")
    end
  end
end

```

This works fine when creating a new user. However it fails when a  
user wants to change their password.

The reason it fails is that the update/edit form view does _not_ let  
the user change their username (usernames are static). So when the  
validate method (above) is ran it does not know what the username is.

What is the rails-way for tackling this?

thanks!

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 9, 2006, 6:45am UTC](https://www.ruby-forum.com/t/help-validating-when-updating-a-record/71467/2 "2006-10-09T06:45:00Z")

</div>

Well, I’ve partly fixed it by including a username hidden field.

That way the username pararmeter will always be passed

Not sure if thats the rails-way or not? 🙂

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [October 9, 2006, 6:49am UTC](https://www.ruby-forum.com/t/help-validating-when-updating-a-record/71467/3 "2006-10-09T06:49:01Z")

</div>

On 10/9/06, Jason B [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > errors.add(:password, "Cannot be the username")
> > 
> > ```
> > 
> > validate method (above) is ran it does not know what the username is.
> > 
> > What is the rails-way for tackling this?
> > 
> > thanks!

I would think that if your table has a username field (called username)  
in  
it then this would not be a problem.

Can you post some more information on your model? Relationships /  
validtaions. Not sure if it will help but some more information would  
be  
good.

Cheers
