# Polymorphic Associations and Validation

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-and-validation/90716>\
**Category:** Rails\
**Created:** [March 31, 2007, 11:50am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-validation/90716 "2007-03-31T11:50:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2007, 11:50am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-validation/90716/1 "2007-03-31T11:50:58Z")

</div>

Hi!

I want to validate values from a form. That wouldn’t be very  
difficult, but the model I want to polulate is in a polymorphic  
association!  
Here are my models:

- Document (created\_at, autor, resource\_id, resource\_type)  
\*\* Folder (name)  
\*\* Binary (name, data, content\_type)

So a document can be either a folder or a binary. But here the problem  
begins. Folder and Binary both have a “name”-column (this column is  
not in the “documents”-table), so when users create a folder, they can  
enter a name. When they upload a file, the “name”-column of the  
“binaries”-table will be set automatically to the name of the file the  
user has uploaded. That’s no problem!  
But I want to validate the name for a folder the user enteres. I have  
two seperate forms for folder- and binary-creation, and I simply want  
to validate the presence and the length of a new folder-name. But  
where does the validation go? And how can I display the error-  
messages?  
I tried several things, but when I try to enter a folder without a  
name there will be no new record in “folders” (which is good!) but no  
error-message and an entry in the “documents”-table.  
How can I validate those entries and how can I display error-messages?  
And how can I prevent inserting “empty” entries to the “documents”-  
table?

Thanks for any help,

- Dominik
