3

I have a database I cannot modify, and it contains column names that are... let's just say stupid. Most are fine, but a couple of them have an apostrophe in it. One is called "Spouse's Birthday".

I can connect to the DB fine using establish_connection, and I can even pull off a Model.count. But the moment I try Model.find, it fails with the following error:

/var/lib/gems/2.3.0/gems/activemodel-5.2.2/lib/active_model/attribute_methods.rb:380:in `module_eval': /var/lib/gems/2.3.0/gems/activemodel-5.2.2/lib/active_model/attribute_methods.rb:381: syntax error, unexpected tIDENTIFIER, expecting ')' (SyntaxError)
...       define_method(:'Spouse's Birthday_before_type_cast') ...
...                               ^
/var/lib/gems/2.3.0/gems/activemodel-5.2.2/lib/active_model/attribute_methods.rb:382: syntax error, unexpected tIDENTIFIER, expecting end-of-input
          attribute_before_type_cast("Spouse's Birthday", *args)

The error message is pretty straightforward; the apostrophe in the column name is ruining everything. I need a way to either escape that, or feed establish_connection something that can map the existing column names to the names I would prefer using.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Orlando
  • 1,236
  • 3
  • 12
  • 24

0 Answers0