# Validates\_presence\_of \*\_id attributes

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098>\
**Category:** Rails\
**Created:** [January 3, 2006, 8:34am UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098 "2006-01-03T08:34:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [January 3, 2006, 8:34am UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098/1 "2006-01-03T08:34:58Z")

</div>

Hi all,

I am a newbie to Rails. Please enlighten me on how to do this  
appropriately, the Rails and the Ruby way:

Suppose I have a Recipe model. Let’s simplify things and pretend that  
it has only 2 attributes, a :name and the other is a ‘category\_id’. In  
the recipes table, category\_id is a foreign key to field id of table  
categories.

We also assume that I have generate the appropriate scaffolding code  
for Recipe and Category. All goes well if I go to  
[http://localhost:3000/categories](http://localhost:3000/categories) (I’m using WEBrick) and add a new  
Category and then go to [http://localhost:3000/recipes](http://localhost:3000/recipes) and add a Recipe  
and choose a category for the recipe.

What if I go to [http://localhost:3000/recipes](http://localhost:3000/recipes) first and add a new  
recipe without specifying a category (note that I enforce the  
category\_id field in the recipes table to be not null). Rails will  
definitely complain.

Is there somekind of “validates\_presence\_of” for \*\_id kinds of field  
thus the user of my application can be notified to make a category  
first before adding recipe?

Tq very much,  
John

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [January 3, 2006, 8:59am UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098/2 "2006-01-03T08:59:36Z")

</div>

John,

Check out ‘validates\_associated’

[http://api.rubyonrails.com/classes/ActiveRecord/Validations/](http://api.rubyonrails.com/classes/ActiveRecord/Validations/)  
ClassMethods.html#M000689

Chris Nolan.ca  
[http://kweschun.com/](http://kweschun.com/) - Do you have a kweschun?

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [January 3, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098/3 "2006-01-03T12:13:32Z")

</div>

On 1/3/06, Chris Nolan.ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> John,  
> Check out ‘validates\_associated’  
> Chris Nolan.ca

Hey, thanks for the response. Thanks for introducing me to  
validates\_associated too. However, after updating my Recipe model  
with:  
validates\_associated :category  
, the error is still there. I’m afraid that this doesn’t have anything  
to do with validating association, here’s the error:

“You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.inject”

I think this is because I use  
\<%= collection\_select ‘recipe’, ‘category\_id’, @categories, ‘id’,  
‘description’ %\> in \_form.rhtml  
and by the time I intentionally create a new recipe, there is not even  
a single category.

How to handle this the Rails way?  
Tq.

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [January 3, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098/4 "2006-01-03T13:39:09Z")

</div>

in your Recipe model, you just do:

validates\_presence\_of :name, :category\_id

now as far as “before” adding a recipe, the app isn’t going to know you  
haven’t selected anything until you submit the form, then your app will  
do  
the validation before the save. if the validation fails, you can have  
your  
app notify the user using something like:

\<%= error\_messages\_for(:recipe) %\> in your “New Recipe” view.

of course, you could always use javascript to validate your form before  
you  
submit it.

now if your problem is that there are no pre-existing categories, you  
might  
want to think about adding a few default to the app or allow the user to  
create a new category when they are creating a new recipe. if a  
category is  
nothing more than a name like “Dessert” or whatever, just add

\<%= text\_field\_tag “new\_category\_name”, “” %\>

in your create/edit form view and in your create/update controller  
methods,  
do a check on this field…if the user entered a new category, create  
that  
category then use it to create the recipe.

---

<div class="post-metadata">

**Author:** ![John\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@John\_I](https://www.ruby-forum.com/u/John_I)\
**Post date:** [January 3, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/validates-presence-of-id-attributes/56098/5 "2006-01-03T19:28:24Z")

</div>

Chris H. wrote:

> in your Recipe model, you just do:
> 
> validates\_presence\_of :name, :category\_id

Actually, I suggest you use

validates\_presence\_of :category

Because the category association will use the category\_id column to try  
and get  
the Category object, you’ll end up checking if the category\_id is not  
nil and if  
it is not nil, it’ll also try to load the associated row from the  
Category  
table. If that row doesn’t exist for the category\_id, then you’ll also  
get a  
validation error. So you get two checks for one line of code.

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)
