def jsontest @users = User.all.limit(10) render json: @users end
gives
{ ... "id": 7, "name": "Sage Smith", "email": "example-6@railstutorial.org", "created_at": "2013-10-17T02:29:15.638Z", "updated_at": "2013-10-17T02:29:15.638Z", "password_digest": "$2a$10$taHk3udtWN61Il5I18akj.E90AB1TmdL1BkQBKPk/4eZ7YyizGOli", "remember_token": "118f807d0773873fb5e4cd3b5d98048aef4f6f59", "admin": false ... }
But I would like to omit some specific fields from this API, so I use pluck
def jsontest @users = User.all.limit(10).pluck(:id, :name, :email, :created_at)
but pluck only returns an array of values ββwhen I would like to have the attributes of each object accessible with a hash key.
[ ... 7, "Sage Smith", "example-6@railstutorial.org", "2013-10-17T02:29:15.638Z" ... ]
So, how can I efficiently wrest values ββand their keys?
I understand that I can zip through @users and grab keys before plucking and re-creating the hash, but I expect that there will be some convenient method that will do exactly what I want.
ruby-on-rails activerecord
user Dec 27 '13 at 3:42 on 2013-12-27 03:42
source share