Rails,Grape 从 collection 创建自定义 JSON

Rails, Grape create custom JSON from collection

我开始学习如何使用 Grape。我有 collection 有很多属性,我只想要其中的一些。我做了这样的事情:

get :all_elements do
    [
      my_collection.each do |element|
          {
           id: element.id,
           name: element.name
          }
      end
    ]
end

但是这不起作用。如何从 collection 创建自定义 json 数组?

请尝试此代码。

list = my_collection.map do |element|
           { :id => element.id,
             :name => element.email
           }
       end

list.to_json