# Uppercase all row columns

**URL:** <https://www.ruby-forum.com/t/uppercase-all-row-columns/142719>\
**Category:** Rails\
**Created:** [July 10, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719 "2008-07-10T20:23:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/1 "2008-07-10T20:23:46Z")

</div>

Hi.

I have to convert the contents of all columns to uppercase before  
creating a row. Is there an easy way to accomplish this so I don’t  
have to go one column at a time and upcase! it?

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/2 "2008-07-10T20:58:58Z")

</div>

> I have to convert the contents of all columns to uppercase before  
> creating a row. Is there an easy way to accomplish this so I don’t  
> have to go one column at a time and upcase! it?

Right a before\_save filter on your model to loop through each of the  
attributes and uppercase them (but only if they are strings!)

-philip

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/3 "2008-07-10T21:11:28Z")

</div>

That’s what I am doing right now. I have a before\_save filter, but I  
am upcasing each one column individually. How would I loop through the  
attributes and check for the attribute type?

Thanks.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/4 "2008-07-10T22:19:25Z")

</div>

Thanks!

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/5 "2008-07-10T23:15:41Z")

</div>

Sorry, I talked to fast. I tried it and after a little bit of  
research, what ‘attributes’ generates is a hash with the name of the  
column and it’s content, not the column type. Any idea how I can get  
that information?

Thanks.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/6 "2008-07-10T21:37:49Z")

</div>

On Jul 10, 2008, at 12:11 PM, pepe wrote:

> That’s what I am doing right now. I have a before\_save filter, but I  
> am upcasing each one column individually. How would I loop through the  
> attributes and check for the attribute type?

Something like this:

p = Product.find(:first)  
p.attributes.each\_pair do |k,v|  
puts “#{k} is a string” if v.is\_a?(String)  
end  
name is a string  
description is a string  
sku is a string

Now, this is in console so you’ll need to twittle ‘p’ to use self and  
obviously change the attributes instead of printing something out, but  
that’s the general idea.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 10, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/uppercase-all-row-columns/142719/7 "2008-07-10T23:24:29Z")

</div>

I think I got it:

e = Employee.new()  
e.attributes.each\_pair do |k,v|  
puts “#{k} is a #{e.column\_for\_attribute(k).klass}”  
end

LNAME is a String  
FNAME is a String  
DOB is a Date

Thanks so much! You put me in the right direction. 🙂

Pepe
