为什么Rails to_json有时包含属性,attributes_cache,changed_attributes等?

时间:2022-12-08 23:21:20

My to_json call sometimes seems to include some extra cruft that I'm not expecting:

我的to_json电话有时似乎包括一些我不期望的额外信息:

{"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"}

What I'm expecting is closer to this:

我期待的更接近于此:

{"response":
  [survey: {
     (...list of key:value pairs...)
   }, 
   survey: {
     (...list of key:value pairs...)
    }
  ],
  "status_code":"200","status_description":"OK"}

As far as I can tell, the extra stuff that shows up is used by AR to keep track of the state of dirty objects, but I'm not sure why it gets picked up by to_json. My code does nothing fancy as far as I can tell:

据我所知,AR使用了额外的东西来跟踪脏对象的状态,但我不确定为什么它被to_json拾取。据我所知,我的代码没有什么花哨的东西:

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

I cannot reproduce it on the Rails console (including a simpler example for legibility, but I get the same result for my actual code):

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

  ?> 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
  >>

Thanks for any insights! I'm sure I can modify my code to find a workaround (if all else fails I can just build the hash I pass to to_json by hand), but I would like to gain some understanding of why this happens, if possible.

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

1 个解决方案

#1


1  

I just had the same problem - and solved it after extensive googling.

我只是遇到了同样的问题 - 并在广泛的谷歌搜索后解决了它。

You have a JSON gem installed (perhaps as a dependency of another gem) and this is determining your to_json output.

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

Remove the gem (if you can) and it'll come good.

删除宝石(如果可以的话)它会变好。

:)

#1


1  

I just had the same problem - and solved it after extensive googling.

我只是遇到了同样的问题 - 并在广泛的谷歌搜索后解决了它。

You have a JSON gem installed (perhaps as a dependency of another gem) and this is determining your to_json output.

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

Remove the gem (if you can) and it'll come good.

删除宝石(如果可以的话)它会变好。

:)