# Bug? saving (valid) updated object results in validation err

**URL:** <https://www.ruby-forum.com/t/bug-saving-valid-updated-object-results-in-validation-err/50549>\
**Category:** Rails\
**Created:** [December 18, 2005, 12:31am UTC](https://www.ruby-forum.com/t/bug-saving-valid-updated-object-results-in-validation-err/50549 "2005-12-18T00:31:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gui\_dos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e79b87/32.png) [@gui\_dos](https://www.ruby-forum.com/u/gui_dos)\
**Post date:** [December 18, 2005, 12:31am UTC](https://www.ruby-forum.com/t/bug-saving-valid-updated-object-results-in-validation-err/50549/1 "2005-12-18T00:31:56Z")

</div>

Hi!

I have an error that I don’t understand. It shouldn’t be happening,  
according to the documentation! I have a class, defined as below.

> validates\_numericality\_of :voucher, :only\_integer =\> true  
> validates\_each :price\_sold do |rec, s, t|  
> t ||= 0; rec.errors.add s, “negative price” if t \< 0  
> end

Using script/console as follows:-

> been taken"]}, @base=#\<Coupon:0x24d3e64 …\>\>, @attributes=  
> {“created\_on”=\>nil, “account\_id”=\>nil, “updated\_on”=\>nil,  
> “product\_id”=\>“11”, “used\_on”=\>nil, “id”=\>“42”, “used”=\>“f”,  
> “voucher”=\>“22334455667788990011”, “price\_sold”=\>“0.0”}, @product=nil\>
> 
> > >

It appears that

a) the coupon is really valid, and is fetched from the database.

b) saving it again does not work, (I don’t know why, especially as  
the API documentation says the below text for save)

c) validates\_uniqueness\_of :voucher appears to be the culprit. I  
removed it and lo and behold, it saved.

> No record exists: Creates a new record with values matching those  
> of the object attributes.  
> A record does exist: Updates the record with values matching those  
> of the object attributes.  
> It should update the record with the new values. I am running into  
> this when I retrieve the object, update some values and when I  
> attempt to save … it gives a  
> “Voucher has already been taken” error message, indicating that it  
> things I am trying to save a duplicate object, when it should instead  
> realize I am using the right object!

Here’s information about my Rails version:-

> Active Support version  
> 1.2.5

How can I get this working yet keep the  
validates\_uniqueness\_of :voucher constraint? Thanks for any help!

– G.
