dewski/json_builder

display collection as json array in rails

joshryanne opened this issue · 1 comments

hi, i'm very new to rails(and ruby). was wondering how i can get the following output in rails:

[
{
"first": "Garrett",
"last": "Bjerkhoel"
},
{
"first": "John",
"last": "Doe"
}
]

** if i do something like

Staff @users do |user|
first user.firstname
last last:lastname
end

** it will give me something like:

{ "staff":[
{
"first": "Garrett",
"last": "Bjerkhoel"
},
{
"first": "John",
"last": "Doe"
}
]
}

** but i don't want to include "staff" etc. i just want a json with an array directly as i mentioned above can you pls. teach me how, as i've said i'm new to ruby and rails.
thanks.

You have to use the array method:

array @users do |user|
  first user.firstname
  last user.lastname
end

Welcome to Rails btw!