# What is wrong with validates\_presence\_of :my\_association?

**URL:** <https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268>\
**Category:** Rails\
**Created:** [January 9, 2009, 1:13am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268 "2009-01-09T01:13:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [January 9, 2009, 1:13am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268/1 "2009-01-09T01:13:53Z")

</div>

Category.has\_many :products

Let’s say a product must belong to a category. I know it is recommended  
to do the following:

class Product  
validates\_presence\_of :category\_id  
end

But, as we all know, this is a pain for new records, meaning what if the  
associated category is a new record?

I’ve been meaning to ask this, but what’s wrong with doing the  
following:

class Product  
validates\_presence\_of :category  
end

I never understood why that was so wrong. So I wanted to ask and see if  
I am missing something. What do you think? Thanks for your help!

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [January 9, 2009, 1:59am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268/2 "2009-01-09T01:59:40Z")

</div>

## That will check the value for a field called category, not an association.

Ryan B.  
Freelancer

> **[frozenplague.net](http://frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [January 9, 2009, 2:52am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268/3 "2009-01-09T02:52:57Z")

</div>

- Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [2009-01-09 01:13:53  
+0100]:

> But, as we all know, this is a pain for new records, meaning what if the  
> associated category is a new record?
> 
> I’ve been meaning to ask this, but what’s wrong with doing the  
> following:
> 
> class Product  
> validates\_presence\_of :category  
> end

In this case you may need validates\_associated or write your own  
validation by def validate.

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [January 9, 2009, 3:03am UTC](https://www.ruby-forum.com/t/what-is-wrong-with-validates-presence-of-my-association/157268/4 "2009-01-09T03:03:29Z")

</div>

Ben J. wrote:

> associated category is a new record?
> 
> I’ve been meaning to ask this, but what’s wrong with doing the  
> following:
> 
> class Product  
> validates\_presence\_of :category  
> end
> 
> I never understood why that was so wrong. So I wanted to ask and see if  
> I am missing something. What do you think? Thanks for your help!

It isn’t wrong, it’s the right way to do it. You just have to  
assign a category object when building a new Product.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
