Can ActiveRecord handle columns containing spaces?

  • Thread starter Anthony Hrvojic
  • Start date
A

Anthony Hrvojic

Hello,

I'm using Rails 0.14.3 (upgraded this morning in case it would solve
this) with an existing SQL Server database and I'm running into a
problem where one of my models fails to instantiate seemingly due to a
single column containing a space in its name, which I guess interferes
with dynamic attribute generation.

The exception I get in my browser looks something like this:

(eval):1:in `class_eval': compile error
(eval):1: formal argument cannot be a constant
def Spaced Column; raise NoMethodError, 'missing attribute: Spaced
Column', caller unless @attributes.has_key?('Spaced Column');
(v=@attributes['Spaced Column']) &&
ActiveRecord::ConnectionAdapters::ColumnWithIdentity.string_to_time(v);
end

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1545:in
`class_eval'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1545:in
`class_eval'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1545:in
`define_read_method'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1530:in
`define_read_methods'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1528:in
`each'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1528:in
`define_read_methods'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:1478:in
`method_missing'

Modifying the schema is probably not an option, so I'm hoping someone
may have a different approach.

Thanks!
 
R

Robert Klemme

Anthony said:
Hello,

I'm using Rails 0.14.3 (upgraded this morning in case it would solve
this) with an existing SQL Server database and I'm running into a
problem where one of my models fails to instantiate seemingly due to a
single column containing a space in its name, which I guess interferes
with dynamic attribute generation.

The exception I get in my browser looks something like this:

(eval):1:in `class_eval': compile error
(eval):1: formal argument cannot be a constant
def Spaced Column; raise NoMethodError, 'missing attribute: Spaced
Column', caller unless @attributes.has_key?('Spaced Column');
(v=@attributes['Spaced Column']) &&
ActiveRecord::ConnectionAdapters::ColumnWithIdentity.string_to_time(v);
end

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1545:in
`class_eval'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1545:in
`class_eval'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1545:in
`define_read_method'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1530:in
`define_read_methods'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1528:in
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1528:in
`define_read_methods'
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.
rb:1478:in
`method_missing'

Modifying the schema is probably not an option, so I'm hoping someone
may have a different approach.

Thanks!

Ypu might get away with creating a view with a different name for this
column. IIRC you can insert into simple views such as

create view foo(col1,col2)
as
select colx, coly
from some_table

It's generally not a good idea to have spaces in identifiers, even if they
are column names - I guess you came to a similar conclusion already... :)

Kind regards

robert
 
A

Anthony Hrvojic

Ah, of course! Works like a charm and at least I have this option now.

I have to admit I was surprised that spaces were even allowed in column
names when I first ran into this :)

Thank you, Robert!
 
J

Jeremy Kemper

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm using Rails 0.14.3 (upgraded this morning in case it would solve
this) with an existing SQL Server database and I'm running into a
problem where one of my models fails to instantiate seemingly due to a
single column containing a space in its name, which I guess interferes
with dynamic attribute generation.

The exception I get in my browser looks something like this:

(eval):1:in `class_eval': compile error
(eval):1: formal argument cannot be a constant
def Spaced Column; raise NoMethodError, 'missing attribute: Spaced
Column', caller unless @attributes.has_key?('Spaced Column');
(v=@attributes['Spaced Column']) &&
ActiveRecord::ConnectionAdapters::ColumnWithIdentity.string_to_time
(v);
end

Anthony, you may turn off reader method generation if you'd like this
to work directly, though you'll lose its substantial speed boost. In
config/environment.rb
config.active_record.generate_read_methods = false

Then you may wrap the offending column with Ruby-friendly method names:
def sane_column
read_attribute('Spaced Column')
end

def sane_column=(value)
write_attribute('Spaced Column', value)
end

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDcP5nAQHALep9HFYRAj1LAKDPVbgecpiDPduT3Hq1dSKOJKMfVQCgjGej
wxO+TsOvQcUKRu8BeSArjbk=
=E41D
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top