# AR/SQLServer: Legacy column names with embedded spaces

**URL:** <https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527>\
**Category:** Rails\
**Created:** [December 6, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527 "2006-12-06T17:22:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 6, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527/1 "2006-12-06T17:22:17Z")

</div>

I’m working against a legacy (which means I can’t change the table or  
move to MySQL ;]) table in a SQL Server database that has column names  
with embedded spaces in them (see error messages below).

When I go to delete one of these records I get messages like the ones  
below for every column with any embedded spaces.

A quick googling reveals that these may just be warnings? Is that  
everyone else’s experience or will the presence of these columns  
actually cause problems when I do DB operations on these type of tables?

Thanks,  
Wes

=================

Exception occured during reader method compilation.  
Maybe Date Code 3 is not a valid Ruby identifier?  
(eval):1:in `evaluate_read_method': compile error (eval):1: formal argument cannot be a constant def Date Code 3; raise NoMethodError, 'missing attribute: Date Code 3', caller unless @attributes.has_key?('Date Code 3'); @attributes['Date Code 3']; end ^ (eval):1: parse error, unexpected tINTEGER, expecting '\n' or ';' def Date Code 3; raise NoMethodError, 'missing attribute: Date Code 3', caller unless @attributes.has_key?('Date Code 3'); @attributes['Date Code 3']; end ^ (eval):1: parse error, unexpected kEND, expecting $ def Date Code 3; raise NoMethodError, 'missing attribute: Date Code 3', caller unless @attributes.has_key?('Date Code 3'); @attributes['Date Code 3']; end ^ Exception occured during reader method compilation. Maybe Date Code 3 is not a valid Ruby identifier? (eval):1:in`evaluate\_read\_method’: compile error  
(eval):1: formal argument cannot be a constant  
def Date Code 3?; query\_attribute(‘Date Code 3’); end  
^  
(eval):1: parse error, unexpected tINTEGER, expecting ‘\n’ or ‘;’  
def Date Code 3?; query\_attribute(‘Date Code 3’); end  
^  
(eval):1: parse error, unexpected kEND, expecting $  
def Date Code 3?; query\_attribute(‘Date Code 3’); end

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 6, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527/2 "2006-12-06T17:40:33Z")

</div>

This doesn’t appear to actually interfere with any DB operations, but  
I’d still like to hear about any adverse effects encountered in the  
field.

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 7, 2006, 11:50am UTC](https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527/3 "2006-12-07T11:50:22Z")

</div>

On 06/12/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m working against a legacy (which means I can’t change the table or  
> move to MySQL ;]) table in a SQL Server database that has column names  
> with embedded spaces in them (see error messages below).

> When I go to delete one of these records I get messages like the ones  
> below for every column with any embedded spaces.

> Exception occured during reader method compilation.  
> Maybe Date Code 3 is not a valid Ruby identifier?  
> (eval):1:in `evaluate\_read\_method’: compile error  
> (eval):1: formal argument cannot be a constant

> def Date Code 3; raise NoMethodError, ‘missing attribute: Date Code 3’,  
> caller unless @attributes.has\_key?(‘Date Code 3’); @attributes[‘Date  
> Code 3’]; end

Wes,

These errors are thrown by ActiveRecord::Base when it tries to create  
attribute accessor methods. As you can see, it’s trying to define a  
method ‘Date Code 3’ which is an invalid method name due to the spaces  
within the name. The fact that DB column names can map to object  
attribute names is a fundamental assumption of ActiveRecord, and hence  
not an easy thing to overcome. The best solution I can think is to  
develop a plugin to map attribute names used in your database to valid  
method names and patch your version of active record to use this  
mapping.

Tom

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [December 7, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/ar-sqlserver-legacy-column-names-with-embedded-spaces/78527/4 "2006-12-07T16:44:47Z")

</div>

Tom W. wrote:

> Wes,
> 
> These errors are thrown by ActiveRecord::Base when it tries to create  
> attribute accessor methods. As you can see, it’s trying to define a  
> method ‘Date Code 3’ which is an invalid method name due to the spaces  
> within the name. The fact that DB column names can map to object  
> attribute names is a fundamental assumption of ActiveRecord, and hence  
> not an easy thing to overcome. The best solution I can think is to  
> develop a plugin to map attribute names used in your database to valid  
> method names and patch your version of active record to use this  
> mapping.

Tom,

Thanks. I wonder how many DBs beside SQL Server actually even allow  
column names to have embedded spaces. Even if it’s only SS, this feels  
like something that should be handled, if only to ease Rails adoption to  
work with legacy schemas. If I get some time, I will look into it.

Wes
