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


#1

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.