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

前端 未结 7 828
予麋鹿
予麋鹿 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:18

    @girishso's gem is great, but my project is in Rails 3. It doesn't work.

    This article is helpful to me, or install pluck_all gem to achieve this.

    Usage:

    User.limit(10).pluck_all(:id, :name, :email, :created_at)
    

提交回复
热议问题