def jsontest
@users = User.all.limit(10)
render json: @users
end
yields
{
...
\"id\": 7,
\"name\": \"Sage Smith\",
\"email\"
You can pull the data along with the column name as:
@users = User.all.limit(10)
.pluck(:id, :name, :email, :created_at)
.map {|id, name, email, created_at| { id: id, name: name,
email: email,
created_at: created_at } }
This will pull the data and map it according to how you want it. One advantage of using pluck
over select
is that you can use joins
along with it.