首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Rails to_json有时会包含attributes、attributes_cache、changed_attributes等?

为什么Rails to_json有时会包含attributes、attributes_cache、changed_attributes等?
EN

Stack Overflow用户
提问于 2010-08-31 01:50:00
回答 1查看 596关注 0票数 0

我的to_json调用有时似乎包含了一些我意想不到的额外麻烦:

代码语言:javascript
复制
{"response":
  [{"attributes":
    {
        (...actual list of key:value pairs, as expected)
    },
    "json_class":"Survey",
    "attributes_cache":{}
   },
   {"changed_attributes":{"survey_source":""},
    "attributes":
    {
     (...actual list of key:value pairs, as expected)
    },
    "json_class":"Survey",
    "attributes_cache":{}}
  ],
  "status_code":"200","status_description":"OK"}

我所期待的更接近于这个:

代码语言:javascript
复制
{"response":
  [survey: {
     (...list of key:value pairs...)
   }, 
   survey: {
     (...list of key:value pairs...)
    }
  ],
  "status_code":"200","status_description":"OK"}

据我所知,显示的额外内容被AR用来跟踪脏对象的状态,但我不确定为什么它会被to_json拾取。据我所知,我的代码并没有什么花哨的功能:

代码语言:javascript
复制
RESPONSE_OK = {"status_code" => "200", "status_description" => "OK"}
@surveys = Survey.find_all_by_state("RUNNING")
response = RESPONSE_OK.merge({"response" => @surveys})
respond_to do |format|
    format.json {
        render :json => response.to_json(:methods => :intercept, :include => {:question_groups => {:include => [:questions]}})
    }
end

我不能在Rails控制台上重现它(包括一个更简单的易读性示例,但我在实际代码中得到了相同的结果):

代码语言:javascript
复制
  ?> s = Survey.find_by_id(49)
  => #<Survey id: 49, name: "Cat?", description: "", created_at: "2010-08-29 17:50:45", updated_at: "2010-08-29 17:50:45", order: nil, entrance_logic: nil, user_id: 187, state: "RUNNING", maxresponses: 0, isrepeatable: false, reward_description: nil, compensation_amount: 0, survey_source: nil, responses_received: nil, is_profile_survey: false, hash_id: "123", survey_alias: "456", prerequisites: nil, is_web_survey: false, max_invites: 0, is_social: true>
  >> s.to_json
  => "{"survey":{"survey_source":null,"prerequisites":null,"name":"Cat?","isrepeatable":false,"maxresponses":0,"hash_id":"123","entrance_logic":null,"created_at":"2010-08-29T17:50:45Z","survey_alias":"456","updated_at":"2010-08-29T17:50:45Z","reward_description":null,"responses_received":null,"order":null,"id":49,"user_id":187,"max_invites":0,"is_web_survey":false,"is_social":true,"is_profile_survey":false,"description":"","compensation_amount":0,"state":"RUNNING"}}"
  >> s.reward_description = "100 points"
  => "100 points"
  >> s.to_json
  => "{"survey":{"survey_source":null,"prerequisites":null,"name":"Cat?","isrepeatable":false,"maxresponses":0,"hash_id":"123","entrance_logic":null,"created_at":"2010-08-29T17:50:45Z","survey_alias":"456","updated_at":"2010-08-29T17:50:45Z","reward_description":"100 points","responses_received":null,"order":null,"id":49,"user_id":187,"max_invites":0,"is_web_survey":false,"is_social":true,"is_profile_survey":false,"description":"","compensation_amount":0,"state":"RUNNING"}}"
  >> s.changed
  => ["reward_description"]
  >> s.changed_attributes
  NoMethodError: Attempt to call private method
          from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:236:in `method_missing'
          from (irb):45
          from :0
  >>

感谢你的见解!我确信我可以修改我的代码以找到解决方法(如果所有其他方法都失败了,我可以手动构建传递给to_json的散列),但如果可能的话,我想了解一下为什么会发生这种情况。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-01 08:35:13

我只是遇到了同样的问题--经过大量的谷歌搜索,我解决了这个问题。

您已经安装了一个JSON gem (可能是另一个gem的依赖项),这决定了您的to_json输出。

移除宝石(如果可以的话),它会变得很好。

:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3602891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档