# Validating if record exists by name

**URL:** <https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262>\
**Category:** Rails\
**Created:** [January 27, 2008, 9:50am UTC](https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262 "2008-01-27T09:50:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 9:50am UTC](https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262/1 "2008-01-27T09:50:34Z")

</div>

hey guys, i have looked alot of places and seen screencasts on  
validation but i still dont understand how to go about validating a  
form to check if the record exists by the name entered in.

lets say a project, has users assigned to the project, and we want to  
assign users by using a textbox for their username.

how would we do the validation in the model?

i know i can simply write an if statement for manually checking the  
textbox value and doing a find in the controller, but it seems a bit  
messy.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262/2 "2008-01-27T14:36:55Z")

</div>

Chubbs wrote:

> hey guys, i have looked alot of places and seen screencasts on  
> validation but i still dont understand how to go about validating a  
> form to check if the record exists by the name entered in.
> 
> lets say a project, has users assigned to the project, and we want to  
> assign users by using a textbox for their username.
> 
> how would we do the validation in the model?
> 
> i know i can simply write an if statement for manually checking the  
> textbox value and doing a find in the controller, but it seems a bit  
> messy.

Try validates\_uniqueness\_of in the model.

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262/3 "2008-01-27T14:44:35Z")

</div>

but thats good for checking if for a user id, but i have the name of  
the user

On Jan 28, 12:36 am, Jeremy Weiskotten \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [January 27, 2008, 3:02pm UTC](https://www.ruby-forum.com/t/validating-if-record-exists-by-name/127262/4 "2008-01-27T15:02:59Z")

</div>

Chubbs wrote:

> but thats good for checking if for a user id, but i have the name of  
> the user
> 
> On Jan 28, 12:36 am, Jeremy Weiskotten \<rails-mailing-l…@andreas-

In your original post, you asked how you could do the validation in the  
model, but I don’t think you want to do validation. You want to do a  
lookup by name in order to link something to a user’s account.

user = User.find\_by\_name(params[:name])
