# "upcasing!" model attributes

**URL:** <https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727>\
**Category:** rubyonrails-talk\
**Created:** [July 11, 2008, 5:50pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727 "2008-07-11T17:50:26Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 11, 2008, 5:50pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/1 "2008-07-11T17:50:26Z")

</div>

Hi.

A couple of days ago I posted a question about how to simplify the task of converting string values to uppercase when saving a row and with some help I got to the point of writing the following in a before\_save method to test the value's contents:

self.attributes.each\_pair do |k,v| &nbsp;&nbsp;puts "Column #{k}: regular value = #{v} - upcase value = #{v.upcase!}" if v.class.name == 'String' end

The problem I am having is that I can use upcase! all I want against 'v' in this case but that will not change the column value, which is what I need to do. I have tried to use self.attribute\_for\_name(k).upcase! and some other things but I either got syntax errors (method not found, etc.) or what I tried did not work. I am relatively new to RoR and any help would be greatly appreciated.

Thanks.

Pepe

---

<div class="post-metadata">

**Author:** ![Reid\_Lynch](https://avatars.discourse-cdn.com/v4/letter/r/ac8455/32.png) [@Reid\_Lynch](https://discuss.rubyonrails.org/u/Reid_Lynch)\
**Post date:** [July 11, 2008, 5:59pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/2 "2008-07-11T17:59:20Z")

</div>

Have you tried explicitly setting v = v.upcase in your before\_save callback?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 11, 2008, 7:51pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/3 "2008-07-11T19:51:52Z")

</div>

pepe wrote:

> self.attributes.each\_pair do |k,v| &nbsp;&nbsp;puts "Column #{k}: regular value = #{v} - upcase value = #{v.upcase!}" if v.class.name == 'String' end

Right. In this case, v is just a local variable of sorts, so it's not going to change the object. You need to do something along the lines of

self.send(k).upcase! if self.send(k).class == String

I just did a quick test in console, and that worked. There might be a better way to do it, though.

Peace, Phillip

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [July 11, 2008, 8:07pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/4 "2008-07-11T20:07:43Z")

</div>

before\_save :upcase\_attributes

private

# Upcase any attributes that are strings

def upcase\_attributes self.attributes.each\_pair do |key, value| self[key] = value.upcase if self[key].is\_a? String end end

That’s probably the most appropriate way to do it. Instead of using the methods, I just access the attributes themselves which can be faster.

Also note that I’m not overriding before\_save directly, but rather using a method call. This way I can do multiple things before saving.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 11, 2008, 9:25pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/5 "2008-07-11T21:25:30Z")

</div>

Hi --

> Also note that I'm not overriding before\_save directly, but rather using a method call. This way I can do multiple things before saving.

You can actually mix and match. If you do this:

class MyModel \< ARB

&nbsp;&nbsp;&nbsp;def before\_save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Instance method" &nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;before\_save :do\_something &nbsp;&nbsp;&nbsp;before\_save { puts "block given to before\_save" } &nbsp;&nbsp;&nbsp;before\_save { puts "another block given to before\_save" } &nbsp;&nbsp;&nbsp;def do\_something &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Method referenced by before\_save" &nbsp;&nbsp;&nbsp;end end

then when you save, you'll get this:

&nbsp;&nbsp;&nbsp;Method referenced by before\_save &nbsp;&nbsp;&nbsp;block given to before\_save &nbsp;&nbsp;&nbsp;another block given to before\_save &nbsp;&nbsp;&nbsp;Instance method

David

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 11, 2008, 9:27pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/6 "2008-07-11T21:27:34Z")

</div>

Hi --

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 11, 2008, 10:27pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/7 "2008-07-11T22:27:11Z")

</div>

Although prior to rails 2.1 fiddling with the attributes hash is a waste of time because it always used to return you a fresh (and deep) copy of the attributes. This changed in rails 2.1

Fred

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 11, 2008, 10:42pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/8 "2008-07-11T22:42:33Z")

</div>

Hi --

> > > Have you tried explicitly setting v = v.upcase in your before\_save callback?
> > 
> > That won't work. It just attaches the identifier 'v' to a new object.
> > 
> > &nbsp;&nbsp;h = { "one" =\> 1 } &nbsp;&nbsp;h.each {|k,v| v = "ONE" } &nbsp;&nbsp;p h # =\> { "one" =\> 1 }
> > 
> > You have to assign to the hash via its keys.
> 
> Although prior to rails 2.1 fiddling with the attributes hash is a waste of time because it always used to return you a fresh (and deep) copy of the attributes. This changed in rails 2.1

I had lapsed into focusing on the assignment semantics per se, but indeed if it's a copy anyway, it won't do any good even if one does assign to it. It doesn't look like it changed in 2.1.0:

&nbsp;&nbsp;&nbsp;def attributes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.attribute\_names.inject({}) do |attrs, name| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs[name] = read\_attribute(name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end

Is there another way in that you're thinking of, or maybe it's on edge?

David

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 12, 2008, 12:39am UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/9 "2008-07-12T00:39:23Z")

</div>

Isn't that ok though (in this particular case), since that's basically &nbsp;&nbsp;&nbsp;def attributes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.attribute\_names.inject({}) do |attrs, name| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs[name] = @attributes[name] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end

ie self.attributes does have as values the same objects that @attributes has as values. We can't changed attributes by assigning to self.attributes, but in place (eg upcase!) modification of self.attributes should work.

On the other hand, before 2.1 it was basically

attrs[name] = @attributes[name].clone

in which case you've haven't got a snowballs chance in hell of changing an attribute by acting on self.attributes.

I think this demonstrates that trying to modify attributes by acting on self.attributes is not a good idea 🙂

Fred

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [July 12, 2008, 12:45am UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/10 "2008-07-12T00:45:36Z")

</div>

Hi --

> > Hi --
> > 
> > I had lapsed into focusing on the assignment semantics per se, but indeed if it's a copy anyway, it won't do any good even if one does assign to it. It doesn't look like it changed in 2.1.0:
> > 
> > &nbsp;&nbsp;def attributes &nbsp;&nbsp;&nbsp;&nbsp;self.attribute\_names.inject({}) do |attrs, name| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs[name] = read\_attribute(name) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> Isn't that ok though (in this particular case), since that's basically &nbsp;&nbsp;def attributes &nbsp;&nbsp;&nbsp;&nbsp;self.attribute\_names.inject({}) do |attrs, name| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs[name] = @attributes[name] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> ie self.attributes does have as values the same objects that @attributes has as values. We can't changed attributes by assigning to self.attributes, but in place (eg upcase!) modification of self.attributes should work.

OK, I see what you mean. I was thinking you meant that assigning to keys would work (e.g., obj.attributes["name"] = "David").

> On the other hand, before 2.1 it was basically
> 
> attrs[name] = @attributes[name].clone
> 
> in which case you've haven't got a snowballs chance in hell of changing an attribute by acting on self.attributes.
> 
> I think this demonstrates that trying to modify attributes by acting on self.attributes is not a good idea 🙂

I agree. Of course there's #attributes=, which thickens the plot a bit.

David

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [July 12, 2008, 4:08am UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/11 "2008-07-12T04:08:59Z")

</div>

Hey, that’s nifty. I love this community 🙂

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 13, 2008, 1:38pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/12 "2008-07-13T13:38:54Z")

</div>

First of all I would like to thank everybody for their help. I have learned quite a few things by reading all the postings. However I got a little lost after Frederick Cheung's posting and if you guys have time I'd appreciate a comment on the following:

1. What is a 'deep' copy of an attribute? 2. What is @attributes? Based on what I know, could it be an instance variable of the model with attributes information? 3. And finally, what is #attributes=? It's the first time I see something like that.

And now to the code:

I first tried Phillip Koebe's code but for some reason it wouldn't work but when I tried Brian Hogan's it surely did the trick. Thanks a lot Brian!

Reid, yes I tried to directly upcase the value but that wouldn't work. I tried pretty much everything I could think of with my limited knowledge of RoR.

Again, thanks a lot for all the help and comments.

Pepe

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [July 13, 2008, 5:40pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/13 "2008-07-13T17:40:07Z")

</div>

> First of all I would like to thank everybody for their help. I have learned quite a few things by reading all the postings. However I got a little lost after Frederick Cheung's posting and if you guys have time I'd appreciate a comment on the following:
> 
> 1. What is a 'deep' copy of an attribute?

Say you have an array and you make a copy of it. That can be two things: - a new array (but which contains the same objects as the old one): a shallow copy - a new array, which contains copies of the contents of the old array: ( a deep copy)

> 2. What is @attributes? Based on what I know, could it be an instance variable of the model with attributes information?

It's where the attributes are actually stored. I would consider this an implementation detail

> 3. And finally, what is #attributes=? It's the first time I see something like that.

The leading hash is a notation for instance methods. It means 'the method that is called when you do someobject.attributes = foo'

Fred

---

<div class="post-metadata">

**Author:** ![pepe1](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@pepe1](https://discuss.rubyonrails.org/u/pepe1)\
**Post date:** [July 14, 2008, 2:20pm UTC](https://discuss.rubyonrails.org/t/upcasing-model-attributes/31727/14 "2008-07-14T14:20:47Z")

</div>

Thanks a lot for the explanations, Fred. I appreciate it. 🙂

Pepe
