简体   繁体   English

避免使用ActiveResource的键的符号

[英]Avoid symbolication of keys for ActiveResource

I am using an ActiveResource model named "Setting" to connect to an external web service which delivers some response like the following example: 我使用名为“Setting”的ActiveResource模型连接到外部Web服务,该服务提供了一些响应,如下例所示:

{"setting" => {"id" => 10, :details => {"10a" => 7, "10b" => 8}}}

The problem is, that ActiveResource tries to symbolize all keys in the details hash but this is not possible and raises a NameError: wrong constant name 10a . 问题是,ActiveResource尝试用符号表示details哈希中的所有键,但这是不可能的,并引发NameError: wrong constant name 10a Is there any chance to prohibit the symbolication or even to avoid the transformation of details to a separate object? 有没有机会禁止符号甚至避免将details转换为单独的对象?

Thanks 谢谢

Have you tried to set your schema manually? 您是否尝试手动设置架构? I don't know if you can set nested attributes like this. 我不知道你是否可以像这样设置嵌套属性。

class Setting < ActiveResource::Base
   schema = {'setting' => {'id' => :integer, :details => {'10a' => :integer, '10b' => :integer}}}
end

I'm assuming the separate object is a HashWithIndifferentAccess? 我假设单独的对象是HashWithIndifferentAccess? Can you try calling #deep_stringify_keys on the details hash before using it? 在使用之前,您可以尝试在详细信息哈希上调用#deep_stringify_keys吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM