Guido Sohne
2005-Dec-17 23:30 UTC
bug? saving (valid) updated object results in validation error
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.> class Coupon < ActiveRecord::Base > belongs_to :product > belongs_to :account > validates_presence_of :product > validates_associated :product > validates_presence_of :voucher > validates_presence_of :price_sold > validates_uniqueness_of :voucher > validates_length_of :voucher, :within => 10 .. 50 > validates_numericality_of :price_sold > 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 > endUsing script/console as follows:-> >> c = Coupon.find :first > => #<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"}> > >> c.save > => false > >> c > => #<Coupon:0x24d3e64 @errors=#<ActiveRecord::Errors:0x24d2514 > @errors={"product"=>["can''t be blank"], "voucher"=>["has already > 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:-> Rails version > 1.0.0 > Active Record version > 1.13.2 > Action Pack version > 1.11.2 > Action Web Service version > 1.0.0 > Action Mailer version > 1.1.5 > Active Support version > 1.2.5How can I get this working yet keep the validates_uniqueness_of :voucher constraint? Thanks for any help! -- G. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails