The following piece of code should return a json:
@series = @series.map do |serie| {
:name => serie.name,
:id => serie.id
}
@series.to_json
It's return is the following:
"[{\"name\":\"Barra\",\"id\":3},{\"name\":\"Botafogo 1\",\"id\":1},{\"name\":\"Botafogo 2\",\"id\":2},{\"name\":\"Tijuca\",\"id\":4}]"
Why is it returning a string instead of a json?
'json' gem is both installed and required at the top of my .rb file
I've just resolved my problem. For some reason as_json
does what I want rather than to_json
. I didn't quite understand why. I'll update my answer if I ever find out.
That's strange, for to_json
worked before I changed my development database form sqlite 3 to postgresql. But I don't think that might be the problem, because it was working nicely with Heroku's postgre database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With