Abramodj
Abramodj

Reputation: 5879

Problem with active record and the before_save filter

I have this code in my model, but something is not working as I expected.

   before_save :destroy_image?, :no_spaces_in_telephone

   def no_spaces_in_telephone
     self.phone.gsub! /\s+/, 'XXX'
     ABLog "Telefono", self.phone
   end

   def ABLog tag, string
     logger.info "\n\n#### #{tag} : \n " + string.to_s + "\n\n"
   end

The Log prints the phone with "XXX" in place of white spaces, but my record does not get updated, and the phone remains the same, with all the withes paces in their place.

What am I doing wrong?

Upvotes: 1

Views: 596

Answers (1)

Sucrenoir
Sucrenoir

Reputation: 3034

def no_spaces_in_telephone
     self.phone = self.phone.gsub /\s+/, 'XXX'
     ABLog "Telefono", self.phone
end

This is because self.phone= is a method.

Upvotes: 2

Related Questions