0

サーバー上の JSON が、Restkit を使用してモバイル アプリで受信したものと異なる理由を理解しようとしています。オブジェクトの JSON 表現を で見るとputs @items.to_json、次のようになります。

[{"item":{"created_at":"2012-04-19T19:42:44Z","id":4,"json_event_id":null,"name":"Biz Cards","theme_id":null,"updated_at":"2012-04-19T19:42:44Z","url":"","user_id":3}},
 {"item":{"created_at":"2012-04-20T19:37:33Z","id":5,"json_event_id":null,"name":"Hummus","theme_id":null,"updated_at":"2012-04-20T19:37:33Z","url":"","user_id":3}},
 {"item":{"created_at":"2012-04-20T20:01:57Z","id":7,"json_event_id":null,"name":"Wine","theme_id":null,"updated_at":"2012-04-20T20:01:57Z","url":"","user_id":3}},
 {"item":{"created_at":"2012-04-25T19:57:33Z","id":9,"json_event_id":null,"name":"this","theme_id":null,"updated_at":"2012-04-25T19:57:33Z","url":"","user_id":3}}]

ただし、クライアント側で受信した JSON を表示すると、ID (ここでは最後の要素) を含むすべてが null の新しい JSON 要素を取得します。

[{"item":{"created_at":"2012-04-19T19:42:44Z","id":4,"json_event_id":null,"name":"Biz Cards","theme_id":null,"updated_at":"2012-04-19T19:42:44Z","url":"","user_id":3}},
 {"item":{"created_at":"2012-04-20T19:37:33Z","id":5,"json_event_id":null,"name":"Hummus","theme_id":null,"updated_at":"2012-04-20T19:37:33Z","url":"","user_id":3}}, 
 {"item":{"created_at":"2012-04-20T20:01:57Z","id":7,"json_event_id":null,"name":"Wine","theme_id":null,"updated_at":"2012-04-20T20:01:57Z","url":"","user_id":3}}, 
 {"item":{"created_at":"2012-04-25T19:57:33Z","id":9,"json_event_id":null,"name":"this","theme_id":null,"updated_at":"2012-04-25T19:57:33Z","url":"","user_id":3}}, 
 {"item":{"created_at":null,"id":null,"json_event_id":null,"name":null,"theme_id":null,"updated_at":null,"url":null,"user_id":3}}]

@items.to_jsonが異なる JSON を返すのはなぜですか?format.json { render json: @items }また、どのように正しいもので応答できます@items.to_jsonか?

編集 @items を入力するコードは次のとおりです。

@items = current_user.items

編集 これは、current_user に基づいて @items を入力した場合にのみ発生するようにも見えます。

@items = current_user.items

すべてを行うだけで、追加の null オブジェクトはありません。

@items = Item.all
4

0 に答える 0