Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before_save, strip a string

I'm trying to strip the whitespaces of the variable Username in my User Model.

I'm using

before_save do
  self.username.strip!
end

but it doesn't seem to work, am i missing something ?

like image 704
Mini John Avatar asked Jan 28 '14 09:01

Mini John


People also ask

How to strip a string with unnecessary spaces in JavaScript?

Method 1: Here, we will be using trim () method. We can strip a string using trim () method and can remove unnecessary trailing and leading spaces and tabs from the string. Method 2: Here we will be making a user-defined function. We will be writing a JavaScript to strip the string and to remove unnecessary spaces.

What is strip () method in JavaScript?

Definition and Usage The strip () method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)

How to remove all the leading and trailing spaces from a string?

Python strip () in-built method, which is used to remove all the leading and trailing spaces from a string (beginning and end (both sides) of a string).

What is the syntax of strip () in Python?

The syntax of strip () is: chars (optional) – a string specifying the set of characters to be removed. If the chars argument is not provided, all leading and trailing whitespaces are removed from the string.


1 Answers

You'd rather update the setter instead of polluting your model with callbacks:

def username=(value)
  self[:username] = value.to_s.strip
end

Btw, I prefer squish

like image 151
apneadiving Avatar answered Oct 01 '22 05:10

apneadiving