# Rescue unique constraint in the model

**URL:** <https://www.ruby-forum.com/t/rescue-unique-constraint-in-the-model/132748>\
**Category:** Rails\
**Created:** [March 24, 2008, 11:31am UTC](https://www.ruby-forum.com/t/rescue-unique-constraint-in-the-model/132748 "2008-03-24T11:31:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatyree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f14d63/32.png) [@tatyree](https://www.ruby-forum.com/u/tatyree)\
**Post date:** [March 24, 2008, 11:31am UTC](https://www.ruby-forum.com/t/rescue-unique-constraint-in-the-model/132748/1 "2008-03-24T11:31:32Z")

</div>

Hi,

I’m trying to validate the uniqueness of a value in a model where a  
user has\_one item. When the record is created, this isn’t a problem  
as I just use validate\_uniqueness\_of. However, on update, I need to  
be able to bill the user the difference between their existing item  
and their new amount:

class Item \< CachedModel  
attr\_accessor :bidup

belongs\_to :user  
…  
validates\_uniqueness\_of :value, :message =\> “Somebody’s already got  
that spot. Nudge it by 1 and try again.”  
…

def after\_save  
deduct\_payment(self.value) if !bidup?  
deduct\_payment(self.bidup) if bidup?  
end

def save  
self.value += self.bidup if bidup?  
begin  
super  
rescue ActiveRecord::StatementInvalid =\> error  
errors.add(:value, “Somebody’s already got that spot. Nudge it  
by 1 and try again.”)  
return false  
end  
end

protected

def bidup?  
!self.bidup.blank?  
end  
end

This works fine on create, but when I call  
Item.update\_attribute(:bidup, ) it raises  
“ArgumentError: wrong number of arguments (1 for 0)”. I’m not deeply  
attached to this way of doing things and would welcome any  
suggestions.

Cheers,  
Todd
