Technical
Dynamic Strip And Gsub

I had a case where i could not present user with <= error_messages_for > so my goal was to restore previous value on blank, strip leading spaces and substitute semicolons from input.


@new_legal_entity_contact = LegalEntityContact.find(params[:id].to_int_key_array)

#Grabs all the fields from model that does not allow null 
#and set them to previous value if blank
LegalEntityContact.column_names.each do |column_name|
  if !LegalEntityContact.columns_hash[column_name].null
   if params[:new_legal_entity_contact][column_name.to_sym].blank?
     params[:new_legal_entity_contact][column_name.to_sym] = @new_legal_entity_contact.send(column_name)
    end
  end
end
#Go through the params hash and gsub as well as strip the strip values
params["new_legal_entity_contact"].each do |key,value|
  if key != "name" && key != "brokerCompanyName" 
   params["new_legal_entity_contact"][key].gsub!("'","") if !validates_numericality_of(value)
  end
 params[:new_legal_entity_contact][key].strip! if !validates_numericality_of(value)
end
@new_legal_entity_contact.update_attributes(params[:new_legal_entity_contact])