Archive for June, 2009

Savings records that fail validation.

Tuesday, June 9th, 2009 By: Daniel

So I had this problem with a record that was being saved even though it failed validation:

cc = CreditCard.new(:card_number => 'invalid')
>> cc.save
=> false
>> cc.id
=> 3

That one had me stumped for a while until I realized I was carelessly using update_attribute in my custom writer:

def card_number=(num)
  update_attribute(:crypted_card_number, encrypt(num))
end

And we all know that update_attribute “saves the record without going through the normal validation procedure.”

Everything worked much nicer after I changed the code to:

def card_number=(num)
  self.crypted_card_number = encrypt(num)
end