samedi 3 novembre 2018

How to strip column values in Active record through loop before saving in rails framework?

I have to remove spaces for every record before saving into the database. I can do that by specifying it for each column, but when there 100 columns in each record, it is a pain and no of lines of code increases enormously.

Need help in changing the code into a loop, but being an amateur in rails I am not able to complete it.

class hello < ActiveRecord::Base

before_validation :remove_spaces  

def remove_spaces
          self.login = self.login.strip if self.login.present?
          self.email = self.email.strip if self.email.present?
          . so on .......

      end

end

Aucun commentaire:

Enregistrer un commentaire