# Embellishing String (or whatever) for AR attributes

**URL:** <https://www.ruby-forum.com/t/embellishing-string-or-whatever-for-ar-attributes/113525>\
**Category:** Rails\
**Created:** [September 1, 2007, 5:08am UTC](https://www.ruby-forum.com/t/embellishing-string-or-whatever-for-ar-attributes/113525 "2007-09-01T05:08:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@noel](https://www.ruby-forum.com/u/noel)\
**Post date:** [September 1, 2007, 5:08am UTC](https://www.ruby-forum.com/t/embellishing-string-or-whatever-for-ar-attributes/113525/1 "2007-09-01T05:08:05Z")

</div>

Forgive my extreme noobishness, but why doesn’t the following work?

class Wombat \< ActiveRecord::Base  
validates\_presence\_of :name

class String  
def normalize  
self.upcase  
end  
end

def name=(name)  
write\_attribute(:name, name.normalize)  
end  
end

Of course I’d want to put the embellishment of String in a module, but  
I’m just trying to get it working. But instead I get:

> > w = Wombat.new(:name =\> “test”)  
> > NoMethodError: undefined method `normalize' for "test":String from (irb):25:in`name=’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
> > active\_record/base.rb:1672:in `send' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1672:in`attributes=’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
> > active\_record/base.rb:1671:in `each' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1671:in`attributes=’  
> > from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/  
> > active\_record/base.rb:1505:in `initialize_without_callbacks' from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:225:in`initialize’  
> > from (irb):29:in `new’  
> > from (irb):29

So what’s the right way to do this? I just want to do better than:

def name=(name)  
write\_attribute(:name, normalize(name))  
end

---

<div class="post-metadata">

**Author:** ![noel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@noel](https://www.ruby-forum.com/u/noel)\
**Post date:** [September 1, 2007, 6:41am UTC](https://www.ruby-forum.com/t/embellishing-string-or-whatever-for-ar-attributes/113525/2 "2007-09-01T06:41:11Z")

</div>

> class Wombat \< ActiveRecord::Base  
> validates\_presence\_of :name
> 
> class String  
> def normalize  
> self.upcase  
> end  
> end

Move this String class block outside of the Wombat class block.

Eventually move it into say RAILS\_ROOT/lib/local/string.rb and then add  
“require ‘local/string’” to your environment.rb or some such.

-philip

---

<div class="post-metadata">

**Author:** ![noel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/0ea827/32.png) [@noel](https://www.ruby-forum.com/u/noel)\
**Post date:** [September 1, 2007, 7:56am UTC](https://www.ruby-forum.com/t/embellishing-string-or-whatever-for-ar-attributes/113525/3 "2007-09-01T07:56:15Z")

</div>

On Sep 1, 12:56 am, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Move this String class block outside of the Wombat class block.
> 
> Eventually move it into say RAILS\_ROOT/lib/local/string.rb and then add  
> “require ‘local/string’” to your environment.rb or some such.
> 
> -philip

Thanks. Perfect.
