# Validates\_presence\_of of polymophic fields

**URL:** <https://www.ruby-forum.com/t/validates-presence-of-of-polymophic-fields/107818>\
**Category:** Rails\
**Created:** [June 26, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/validates-presence-of-of-polymophic-fields/107818 "2007-06-26T15:11:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [June 26, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/validates-presence-of-of-polymophic-fields/107818/1 "2007-06-26T15:11:18Z")

</div>

My models looks like:

class Relationship \< ActiveRecord::Base  
belongs\_to :link\_type  
belongs\_to :parent, :polymorphic =\> true  
belongs\_to :child, :polymorphic =\> true

validates\_presence\_of :link\_type\_id

# validates\_presence\_of :parent\_id

# validates\_presence\_of :parent\_type

# validates\_presence\_of :child\_id

# validates\_presence\_of :child\_type

end

class Person \< ActiveRecord::Base  
has\_many :parents, :as =\> :parent, :class\_name =\> “Relationship”  
has\_many :children, :as =\> :child, :class\_name =\> “Relationship”  
end

class Company \< ActiveRecord::Base  
has\_many :parents, :as =\> :parent, :class\_name =\> “Relationship”  
has\_many :children, :as =\> :child, :class\_name =\> “Relationship”  
end

The four validates\_presence\_of lines that are commented out fail when I  
do a save. I’m doing this in my controller:

```
  relationship = Relationship.new(:link_type => link_type)
  @person.children << relationship
  relationship.parent = company

```

When I do the save, the fields are set properly in the database. Is  
this a bug or am I not doing something right?

Thank you for your help
