Embedded # in column names


#1

I work with some legacy schema’s that have some fairly interesting
column names in tables. Unfortunately, because of limitations in other
programs using the tables, I don’t have the option to change anything
about the tables themselves. I need to figure out how to manage the
column names at a “low level” if possible. In fact, it would be nice to
be able to map an encoded value to/from the table so that at my end of
the world (the Rails end) I don’t have to do anything.

The suggestion is to take a look at activerecord and perhaps encode the
columns/attributes with ‘_pound’ so that a column with the name of emp#
would be encoded as emp_pound. Then Rails wouldn’t be complaining as
much. I have also been given the suggestion that I override the column
names but active record seems to complain regardless. What I see in the
log is:

E:/jruby-1.1.6/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:211:
, unexpected end-of-file
Exception occurred during reader method compilation.
Maybe emp# is not a valid Ruby identifier?

I have a bunch of columns with this naming convention so the more
“global” the solution, the better. My Ruby skills are relatively meager
so I could use some very specific pointers as to how to approach a
solution.

Thanks.

Pete


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#2

On Wed, Feb 11, 2009 at 3:59 PM, Pete H. removed_email_address@domain.invalid wrote:

would be encoded as emp_pound. Then Rails wouldn’t be complaining as much.
use some very specific pointers as to how to approach a solution.
As for a more generic soln, I cannot help you, but your particular
listed error is pretty easy

foo# my comment

The ‘#’ is a comment delimiter.

If you need to supply a Ruby symbol, then something like this should
work:

"my_weird_table#’.to_sym

-Tom

http://xircles.codehaus.org/manage_email


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

Thanks Tom. I had seen that syntax before. The question in my mind is
“Where would a implement the syntax changes?” Would I modify my models?
The error occurs as ActiveRecord reads in the table columns so I would
need to use the syntax in such a way that AR wouldn’t choke on it.

Pete


#4

ActiveRecord lets you overide table and column names, that should be
all you need to do in your model classes that extend
ActiveRecord::Base. Here is one ref I found from 2005
http://jamieorc.wordpress.com/2005/11/30/activerecord-model-legacy-table-column-names/
I know the table name is easily changed via set_table_name, but the
columns I also thought could be as well. Good luck.

On Wed, Feb 11, 2009 at 6:40 PM, Thomas E Enebo removed_email_address@domain.invalid
wrote:

certainly sucks even if not a bug). It is possible there is some

Pete
tables themselves. I need to figure out how to manage the column names at a
E:/jruby-1.1.6/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:211:
listed error is pretty easy


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#5

On Wed, Feb 11, 2009 at 5:27 PM, Pete H. removed_email_address@domain.invalid wrote:

Thanks Tom. I had seen that syntax before. The question in my mind is
“Where would a implement the syntax changes?” Would I modify my models? The
error occurs as ActiveRecord reads in the table columns so I would need to
use the syntax in such a way that AR wouldn’t choke on it.

Good question. In glancing at Rails code it is trying to generate
Rails helper methods and it is choking because the code it is
generating is not savvy enough to deal with a name with a ‘#’ in it.
This may or may not be a bug in Rails (well the error message
certainly sucks even if not a bug). It is possible there is some
activerecord syntax which allows you to alias this somehow…don’t
know…I am guess we are back to your original question again…

A second possible solution could be to make a view or synonym with
a more friendly name maybe? You would need proper rights to db to
make those additions, but they should be non-invasive enough.

-Tom

names in tables. Unfortunately, because of limitations in other programs
active record seems to complain regardless. What I see in the log is:


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: removed_email_address@domain.invalid , removed_email_address@domain.invalid


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email