# Belongs\_to validating presence of foreign key

**URL:** <https://www.ruby-forum.com/t/belongs-to-validating-presence-of-foreign-key/93075>\
**Category:** Rails\
**Created:** [April 27, 2007, 5:49am UTC](https://www.ruby-forum.com/t/belongs-to-validating-presence-of-foreign-key/93075 "2007-04-27T05:49:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zmoazeni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/3da27b/32.png) [@zmoazeni](https://www.ruby-forum.com/u/zmoazeni)\
**Post date:** [April 27, 2007, 5:49am UTC](https://www.ruby-forum.com/t/belongs-to-validating-presence-of-foreign-key/93075/1 "2007-04-27T05:49:04Z")

</div>

I’ve recently read  
[http://blog.teksol.info/articles/2006/03/08/do-not-validate-belongs\_to-or-else](http://blog.teksol.info/articles/2006/03/08/do-not-validate-belongs_to-or-else)  
and I seem to be experiencing conflicting behavior with Rails 1.2.3

Parent Table:  
id Integer  
name String

Child Table:  
id Integer  
name String  
parent\_id Integer

class Parent \< ActiveRecord::Base  
has\_many :children  
end

class Child \< ActiveRecord::Base  
belongs\_to :parent  
validates\_presence\_of :parent\_id  
end

So from reading the link above, as well as the warning on  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000941](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M000941)  
I figured this would work.

parent = Parent.new(:name =\> “Dad”)  
child = parent.children.build(:name =\> “Son”)  
parent.save!

However I consistently get “ActiveRecord::RecordInvalid: Validation  
failed: Children is invalid”

This behavior is exhibited with both “validates\_presence\_of :parent”  
and “:parent\_id”

However, if I remove the “validates\_presence\_of :parent\_id” both  
elements are saved and the Child has been saved with the parent id  
appropriately.

Am I mis-reading something, or did this work in previous versions?

Running on Intel Mac, against both SQLite3 and MySQL.

-Zach
