# Validating the presence of a foreign key?

**URL:** <https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797>\
**Category:** Rails\
**Created:** [December 12, 2007, 9:08am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797 "2007-12-12T09:08:19Z")\
**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:** [December 12, 2007, 9:08am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797/1 "2007-12-12T09:08:19Z")

</div>

In the docs it says I should validate the presence of the foreign key  
and not the object itself. I did this and I still get the error when  
both objects are new records. A very simple example:

class UserGroup \< ActiveRecord::Base  
has\_many :users, :dependent =\> :destroy  
end

class User \< ActiveRecord::Base  
belongs\_to :user\_group  
validates\_presence\_of :user\_group\_id  
end

user\_group = UserGroup.new  
user\_group.users.build  
user\_group.save!

Tells me that users is invalid and it says that user group cant be  
blank. In the docs it says if I do this I should not get this error. Is  
this a new edition to ActiveRecord or something? I am using edge rails.  
Thanks!

---

<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:** [December 12, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797/2 "2007-12-12T18:45:41Z")

</div>

The problem is that user\_group.id is nil, so when build is called, the  
child User object that’s created has a nil foreign key. The save on  
the next line fails for this reason. If you replace UserGroup.new with  
UserGroup.create!, I’ll bet it would work.

That’s my take, at least, and it matches the behavior I’ve seen: You  
have to save the new parent before adding children. This does not  
match my understanding of the Agile book, but I’ve written some tests,  
and that’s what seems to be necessary.

///ark

---

<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:** [December 13, 2007, 3:52am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797/3 "2007-12-13T03:52:51Z")

</div>

Ben J. wrote:

> belongs\_to :user\_group  
> blank. In the docs it says if I do this I should not get this error. Is  
> this a new edition to ActiveRecord or something? I am using edge rails.  
> Thanks!

To my mind the API doc’s advice to validate presence of foreign keys  
rather than associations is wrong, as I have written about before:

[http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767](http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767)

Validating the presence of associations is the Rails equivalent of  
DB fk constraints. Validating the presence of foreign\_key attributes  
just checks that they’re not null. They could still point to  
non-existent records.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<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:** [December 13, 2007, 9:50am UTC](https://www.ruby-forum.com/t/validating-the-presence-of-a-foreign-key/122797/4 "2007-12-13T09:50:35Z")

</div>

On Thu, 13 Dec 2007 13:52:27 +1100, Mark Reginald J. wrote:

> [http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767](http://groups.google.com/group/rubyonrails-talk/msg/0c827209dd10e767)
> 
> Validating the presence of associations is the Rails equivalent of DB fk  
> constraints. Validating the presence of foreign\_key attributes just  
> checks that they’re not null. They could still point to non-existent  
> records.

Ohh, thanks for the information.

-Thufir
