1

我正在尝试格式化 json 响应,删除不需要的字段以获得较小的响应(即 created_at 和 updated_at)

我目前正在这样做:

friends = @user.friends
friends.each do |f|
  f[:name] = f.user.username # adding arbitrary attribute needed in the response
end
render :json => friends.as_json(:except => [:created_at, :updated_at])

created_at 和 updated_at 仍然包含在响应中,我做错了什么?

4

1 回答 1

4

:except不会处理嵌套在数组中的属性。您可以在实例本身上使用它:

friends = @user.friends.map do |f|
  f[:name] = f.user.username # adding arbitrary attribute needed in the response
  f.as_json(:except => [:created_at, :updated_at]
end
render :json => friends
于 2012-07-09T16:54:14.740 回答