Legacy fieldnames in model

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

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.

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

self. worked great. Thanks for the help!

Shauna

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)
(See what readers are saying! 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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs