# Object saved before validated?

**URL:** <https://www.ruby-forum.com/t/object-saved-before-validated/143873>\
**Category:** Rails\
**Created:** [July 23, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/object-saved-before-validated/143873 "2008-07-23T12:35:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [July 23, 2008, 12:35pm UTC](https://www.ruby-forum.com/t/object-saved-before-validated/143873/1 "2008-07-23T12:35:31Z")

</div>

Hey,

I’m having a weird problem: When I add a product via webform it’s being  
saved before validated.

Example: I fill out the form for a product named “Test”, I forget to  
type in the price so the error “Price can’t be blank” and it returns to  
the form. After I typed in a price it says “Product ‘Test’ already  
exists” because it’s been saved the first time already.

Controller:  
def create\_product  
Product.prepare\_to\_save(params[:product])  
@product = Product.new(params[:product])  
respond\_to do |format|  
if @product.save  
flash[:notice] = ‘Product was successfully created.’  
format.html { redirect\_to :action =\> ‘index’ }  
format.xml { render :xml =\> @product, :status =\> :created,  
:location =\> @product }  
else  
format.html { render :action =\> “new\_product” }  
format.xml { render :xml =\> @product.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

Model:  
…  
validates\_presence\_of :title, :description, :image, :category  
validates\_numericality\_of :price  
validates\_uniqueness\_of :title  
…

Has anyone an idea why this is happening?
