Polymorphic Associations and Validation


#1

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