Working with ActiveRecord and columns with apostrophes





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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.










share|improve this question




















  • 1





    It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

    – mu is too short
    Jan 4 at 4:14











  • Or perhaps #{name.to_s.inspect} to be more careful.

    – mu is too short
    Jan 4 at 4:17











  • A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

    – mu is too short
    Jan 4 at 21:38


















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.










share|improve this question




















  • 1





    It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

    – mu is too short
    Jan 4 at 4:14











  • Or perhaps #{name.to_s.inspect} to be more careful.

    – mu is too short
    Jan 4 at 4:17











  • A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

    – mu is too short
    Jan 4 at 21:38














3












3








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.










share|improve this question
















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.







ruby activerecord activemodel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 4:05









mu is too short

355k58702677




355k58702677










asked Jan 3 at 21:35









OrlandoOrlando

6311618




6311618








  • 1





    It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

    – mu is too short
    Jan 4 at 4:14











  • Or perhaps #{name.to_s.inspect} to be more careful.

    – mu is too short
    Jan 4 at 4:17











  • A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

    – mu is too short
    Jan 4 at 21:38














  • 1





    It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

    – mu is too short
    Jan 4 at 4:14











  • Or perhaps #{name.to_s.inspect} to be more careful.

    – mu is too short
    Jan 4 at 4:17











  • A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

    – mu is too short
    Jan 4 at 21:38








1




1





It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

– mu is too short
Jan 4 at 4:14





It is strange, they check if they can "def #{name}(*args)" and the blindly go with "define_method(:'#{name}') do |*args|" without escaping name at all. You don't really need to pass a Symbol to define_method so I wonder if simply "define_method(#{name.inspect}) do |*args|" would do instead. Of course, this would require patching ActiveModel...

– mu is too short
Jan 4 at 4:14













Or perhaps #{name.to_s.inspect} to be more careful.

– mu is too short
Jan 4 at 4:17





Or perhaps #{name.to_s.inspect} to be more careful.

– mu is too short
Jan 4 at 4:17













A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

– mu is too short
Jan 4 at 21:38





A quick'n'dirty change to %Q(define_method(#{name.to_s.inspect}) do |*args|) in the source lets you say things like class C; include ActiveModel::Attributes; attribute "pan'cakes"; end and then work with pan'cakes and pan'cakes= using send and attributes just fine. I'm not sure about a lot of thing here but maybe you could try patching your local ActiveModel and seeing what explodes next.

– mu is too short
Jan 4 at 21:38












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030125%2fworking-with-activerecord-and-columns-with-apostrophes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030125%2fworking-with-activerecord-and-columns-with-apostrophes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'