# Should I validate the presence of a foreign key?

**URL:** <https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538>\
**Category:** Rails\
**Created:** [December 1, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538 "2005-12-01T15:56:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 1, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538/1 "2005-12-01T15:56:51Z")

</div>

Pretty quick example, imagine we have two classes, a user and a post.  
class Post \< ActiveRecord::Base  
belongs\_to :user  
validates\_presence\_of :user\_id  
end

This basically just ensures that no post is created without a user id.  
However if I do something like this (assuming a User has\_many :posts)  
user.posts \<\< Post.new({ :subject = “post subject” })  
user.save

Then I get an error saying “Posts is invalid.” Take out the  
validates\_presence\_of :user\_id and it works fine.

Should I not be validating the presence of foreign keys like this?  
What’s the best way to ensure that a fkey is set up - or is there even  
any reason to? I have constraints set up in my DB, but I was  
wondering if I should/need to make a check in Rails. Am I good to go  
as long as I set relations properly, i.e. user.posts \<\< Post.new…  
post.user = thisuser, etc, rather than accessing the id directly.

Thanks,  
Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 2, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538/2 "2005-12-02T14:54:25Z")

</div>

On 12/1/05, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Then I get an error saying “Posts is invalid.” Take out the  
> validates\_presence\_of :user\_id and it works fine.
> 
> Should I not be validating the presence of foreign keys like this?  
> What’s the best way to ensure that a fkey is set up - or is there even  
> any reason to? I have constraints set up in my DB, but I was  
> wondering if I should/need to make a check in Rails. Am I good to go  
> as long as I set relations properly, i.e. user.posts \<\< Post.new…  
> post.user = thisuser, etc, rather than accessing the id directly.

Do you get the same negative results when using user.posts.create() or  
user.posts.build()? I’m not sure if your example is supposed to work  
since I find the API docs a bit ambiguous when it comes to what the  
different methods defined by the has\_many macro are supposed to do.

Hoping this helps,

- Rowan

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 2, 2005, 4:02pm UTC](https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538/3 "2005-12-02T16:02:21Z")

</div>

perhaps the problem lies in the definition of your users class. Can you  
post the code from this class?

-steven

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [December 4, 2005, 6:25pm UTC](https://www.ruby-forum.com/t/should-i-validate-the-presence-of-a-foreign-key/51538/4 "2005-12-04T18:25:55Z")

</div>

def User \< ActiveRecord::Base  
has\_many :posts  
end

Complex enough? 🙂

Pat
