# Mon proprety isn't a string?

**URL:** <https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455>\
**Category:** Ruby\
**Created:** [February 14, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455 "2010-02-14T21:37:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455/1 "2010-02-14T21:37:11Z")

</div>

Hi,  
I would simply like to remove spaces of the property “name” before save  
and update.  
So i did the code below, but i get this error when I save or update a  
tag.  
#error  
undefined method `strip’ for nil:NilClass

#Tag class  
class Tag \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :students

attr\_accessible :name

validates\_uniqueness\_of :name

before\_save :remove\_spaces  
before\_update :remove\_spaces

private  
def remove\_spaces  
name = name.strip  
end

end

This is how i save or update my tag  
#From another class  
def tag\_attributes=(tags\_att)  
splits = tags\_att.split(’,’)  
splits.each do |tag|  
if !tags.find\_by\_name(tag)  
tags.build(:name =\> tag)  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455/2 "2010-02-14T22:11:29Z")

</div>

Thanks Rob

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 9:56pm UTC](https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455/3 "2010-02-14T21:56:44Z")

</div>

On Feb 14, 2010, at 3:37 PM, Greg Ma wrote:

> class Tag \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :students
> 
> attr\_accessible :name
> 
> validates\_uniqueness\_of :name
> 
> before\_save :remove\_spaces  
> before\_update :remove\_spaces  
> before\_save will catch both create and update
> 
> private  
> def remove\_spaces  
> name = name.strip  
> self.name = self.name.strip

the "name = " part is enough to trick the interpreter into treating  
‘name’ as a local rather than a bare method call. Using self.name  
forces the treatment as a method.

-Rob

> ## splits.each do |tag| if !tags.find\_by\_name(tag) tags.build(:name =\> tag) end end end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [February 14, 2010, 10:47pm UTC](https://www.ruby-forum.com/t/mon-proprety-isnt-a-string/183455/4 "2010-02-14T22:47:47Z")

</div>

Or perhaps clearer:

def remove\_spaces  
name.strip!  
end

If there’s any chance that name could actually be nil, then

def remove\_spaces  
name.strip! if name  
end
