How can I have ActiveRecord's pluck also return the column name for rendering in json?

前端 未结 7 881
予麋鹿
予麋鹿 2020-12-29 02:34
def jsontest
   @users = User.all.limit(10)
   render json: @users
end

yields

{
...

\"id\": 7,
\"name\": \"Sage Smith\",
\"email\"         


        
7条回答
  •  醉话见心
    2020-12-29 03:23

    @andrewCone - It should be like this:

    User.limit(:10).pluck_to_hash([:id, :name, :email, :created_at])
    

提交回复
热议问题