# Legacy fieldnames in model

**URL:** <https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084>\
**Category:** Rails\
**Created:** [February 28, 2007, 2:14am UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084 "2007-02-28T02:14:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [February 28, 2007, 2:14am UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084/1 "2007-02-28T02:14:42Z")

</div>

If I were creating a Rails app from scratch I would use Rails’s naming  
conventions for fieldnames (such as first\_name). However, I am doing a  
prototype for a legacy app that has fieldnames like FirstName and for  
other reasons need to keep them that way. Trust me on that.

The validates\_ series works fine with these legacy fieldnames, as do my  
views and controllers. But when I try to add some methods to my model  
that manipulate the names, Rails balks. For example,  
def full\_name  
FirstName + ’ ’ +  
(if MI then MI + '. ’ else ‘’ end) +  
LastName  
end

This is based on an example from Ruby For Rails (p. 84). Rails gives me  
“NameError in LoansController#list … uninitialized constant FirstName  
… This error occured while loading the following files:  
first\_name.rb”.

I tried putting single quotes around ‘FirstName’ (that turned it into a  
literal), using it as a symbol :FirstName, and putting brackets around  
it. I know the syntax itself is okay because if I substitute  
created\_on.to\_s for FirstName then that part is properly rendered by the  
view.

I understand that Rails is supposed to favor convention but allow you to  
deviate if you must. So how do I refer to my nonstandard fieldnames  
within model methods?

Thanks,  
Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [February 28, 2007, 4:16am UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084/2 "2007-02-28T04:16:13Z")

</div>

Shauna wrote:

> ```
> (if MI then MI + '. ' else '' end) +
> 
> ```
> 
> it. I know the syntax itself is okay because if I substitute  
> created\_on.to\_s for FirstName then that part is properly rendered by the  
> view.
> 
> I understand that Rails is supposed to favor convention but allow you to  
> deviate if you must. So how do I refer to my nonstandard fieldnames  
> within model methods?

You can use either self.FirstName or self[:FirstName].

Or you can create a set of facade accessor methods, perhaps in a  
loop, using the underscore method to de-camelize every field name.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [February 28, 2007, 6:00am UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084/3 "2007-02-28T06:00:26Z")

</div>

This has more to do with Ruby syntax than with Rails per se.  
FirstCaps indicates constants. I don’t know what the “correct”  
response is to this, but one option is to use self.PropertyName  
instead…I did some testing and think that should work so long as the  
attributes are public

Ethan

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [February 28, 2007, 6:17am UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084/4 "2007-02-28T06:17:46Z")

</div>

self. worked great. Thanks for the help!

Shauna

---

<div class="post-metadata">

**Author:** ![shauna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@shauna](https://www.ruby-forum.com/u/shauna)\
**Post date:** [February 28, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/legacy-fieldnames-in-model/87084/5 "2007-02-28T12:51:01Z")

</div>

Hi –

On Wed, 28 Feb 2007, Mark Reginald J. wrote:

> > def full\_name  
> > I tried putting single quotes around ‘FirstName’ (that turned it into a
> 
> Or you can create a set of facade accessor methods, perhaps in a  
> loop, using the underscore method to de-camelize every field name.

Another choice is: FirstName() which will force interpretation of  
FirstName as a method name. (I prefer self.FirstName – I hate empty  
parentheses – but I just thought I’d mention this one too.)

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
