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, <some integer>) 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