# Polymorphic validation

**URL:** <https://www.ruby-forum.com/t/polymorphic-validation/107141>\
**Category:** Rails\
**Created:** [June 18, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/polymorphic-validation/107141 "2007-06-18T15:49:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [June 18, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/polymorphic-validation/107141/1 "2007-06-18T15:49:40Z")

</div>

Hello

I have 2 models.

link.rb

has\_many :categories, :as =\> :categorized  
validates\_presence\_of :name, :url, :created\_at, :category

category.rb

belongs\_to :categorized, :polymorphic =\> true  
validates\_presence\_of :name

Everything seems to work. I select my category from a select tag. The  
problem is i don’t know how to validate if category is for example empty  
(nothing in a select tag), i would like to display the validation error  
along with other validation errors for link.

My create method from links\_controller.rb

def create  
@link = Link.new(params[:link])

```
if @link.save
flash[:notice] = 'Link was successfully created.'
category = Category.new(params[:category][:id])
category.categorized = @link
category.save
else
render :action => "new"
end

```

end
