# Validating the presence of foreign keys /associated objects

**URL:** <https://www.ruby-forum.com/t/validating-the-presence-of-foreign-keys-associated-objects/188149>\
**Category:** Rails\
**Created:** [May 6, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/validating-the-presence-of-foreign-keys-associated-objects/188149 "2010-05-06T16:21:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [May 6, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/validating-the-presence-of-foreign-keys-associated-objects/188149/1 "2010-05-06T16:21:29Z")

</div>

hi guys,

I been trying to find out on how to validate the presence of foreign  
keys/associated objects and the threads that talk about this are  
rather dated and unanswered. I looked at

1. 

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/f8175db2a8321750/d565496a3672f19f?lnk=gst&q=validates+presence+foreign#d565496a3672f19f](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/f8175db2a8321750/d565496a3672f19f?lnk=gst&q=validates+presence+foreign#d565496a3672f19f)  
2)  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/cff66eead0e5580b/c032b19f1832b2e0?lnk=gst&q=validates+presence+foreign#c032b19f1832b2e0](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/cff66eead0e5580b/c032b19f1832b2e0?lnk=gst&q=validates+presence+foreign#c032b19f1832b2e0)

Consider this:

class Part \< ActiveRecord::Base  
belongs\_to :brand  
belongs\_to :category  
belongs\_to :sub\_category  
belongs\_to :user  
belongs\_to :status  
has\_many :fits  
has\_many :images, :dependent =\> :destroy

validates\_presence\_of :title, :description, :category, :location,  
:contact\_name, :contact\_number, :condition  
end

class Brand \< ActiveRecord::Base  
has\_many :designs  
validates\_presence\_of :name  
end

class Fit \< ActiveRecord::Base  
belongs\_to :Part  
belongs\_to :Brand

```
validates_presence_of :part_id

```

end

When I hit the “submit” button to purposely try and create a new  
“Fit” object without selected a Brand (the brand is a drop down box)  
and the Part ID is a hidden field, no errors come up.

How do I validate the presence of the “Part ID” (or part object)  
within the Fit model?  
I tried putting " validates\_presence\_of :part\_id" and it does not  
work.

Any ideas, guys?

thanks

---

<div class="post-metadata">

**Author:** ![ct9a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@ct9a](https://www.ruby-forum.com/u/ct9a)\
**Post date:** [May 6, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/validating-the-presence-of-foreign-keys-associated-objects/188149/2 "2010-05-06T19:25:20Z")

</div>

On Thu, May 6, 2010 at 10:20 AM, ct9a [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Fit \< ActiveRecord::Base  
> belongs\_to :Part  
> belongs\_to :Brand

It probably doesn’t make a difference, but conventionally the  
attribute ids should be lowercase

```
 belongs_to :part
 belongs_to :brand

```

> validates\_presence\_of :part\_id

The association name is :part not :part\_id  
so

```
validates_presence_of :part

```

HTH

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
