# Capitalizing an attribute - view, controller, or model?

**URL:** <https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376>\
**Category:** Rails\
**Created:** [December 7, 2007, 2:21am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376 "2007-12-07T02:21:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 7, 2007, 2:21am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376/1 "2007-12-07T02:21:17Z")

</div>

The users will enter students names on forms. How do I intercept the  
first and last names and have them stored capitalized when the user  
inputs them in lower case?

Do I mess with the form html, skipping form\_for helpers and using  
conditional clauses in the view?

Do I need to separate out the individual attribute and update it  
separately in the controller actions that save/updates the  
record?(instead of the blanket object.update\_attributes(params[:object])  
)

Or, and I think this is what I seem to recall seeing, can I create a  
definition in the model that automatically capitalizes given attributes?

I would like code below to work in the Student model(unfortunately its  
not working!(and creates a “stack level too deep” error if i leave out  
“self”))

def self.first\_name  
first\_name.capitalize  
end

Am I close?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 7, 2007, 2:26am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376/2 "2007-12-07T02:26:28Z")

</div>

On Dec 7, 2007 12:21 PM, Tom N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> record?(instead of the blanket object.update\_attributes(params[:object])  
> first\_name.capitalize  
> end
> 
> ## Am I close?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

To overwrite an accessor setup by AR you need to use a different syntax

def first\_name  
self[:firstname] = self[:firstname].capitalize\_first\_letter\_method  
end

You know though you can do that with css…

HTH

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 7, 2007, 2:46am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376/3 "2007-12-07T02:46:18Z")

</div>

hmm,

I don’t think I really hope to change just what is displayed, or change  
what is pulled out over and over again (thats what the accessor would  
do?)

I’d prefer to change what is stored ? (thanks for pointing out the  
correct way of changing what is retrieved though!)

There must be an idiomatic way of filtering and altering form data going  
into records. Is it just to individually set each attribute in the  
controller?

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 7, 2007, 3:43am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376/4 "2007-12-07T03:43:31Z")

</div>

Thanks Steve!

should have thought of before filters before ; ) … guess thats why  
they’re there for.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 7, 2007, 2:59am UTC](https://www.ruby-forum.com/t/capitalizing-an-attribute-view-controller-or-model/122376/5 "2007-12-07T02:59:11Z")

</div>

On Dec 6, 2007, at 5:46 PM, Tom N. wrote:

> There must be an idiomatic way of filtering and altering form data  
> going  
> into records. Is it just to individually set each attribute in the  
> controller?

class MyModel \< AR::Base  
before\_save :capitalize\_fields  
before\_update :capitalize\_fields

private  
def capitalize\_fields  
[:first\_name, :last\_name].each {|field| self[field] = self  
[field].capitalize}  
end  
end
