# Ruby array, javascript and json issue

**URL:** <https://www.ruby-forum.com/t/ruby-array-javascript-and-json-issue/186198>\
**Category:** Rails\
**Created:** [April 2, 2010, 12:29am UTC](https://www.ruby-forum.com/t/ruby-array-javascript-and-json-issue/186198 "2010-04-02T00:29:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 2, 2010, 12:29am UTC](https://www.ruby-forum.com/t/ruby-array-javascript-and-json-issue/186198/1 "2010-04-02T00:29:28Z")

</div>

Hey all,

I am unable to get a highcharts plugin to render a chart in a rails  
application: [GitHub - loudpixel/highcharts-rails: A plugin for displaying Highcharts graphs in Rails](http://github.com/loudpixel/highcharts-rails)

I believe it has something to do with the sql queries to the database  
placed in a ruby array, which the javascript is unable to intepret. This  
is what I have:

def panels  
pass = Student.find\_by\_sql(‘SELECT COUNT(_) FROM students WHERE  
student\_state = 1’)  
fail = Student.find\_by\_sql('SELECT COUNT(_) FROM students WHERE  
student\_state = 2’)

student\_data = [  
{:name =\> ‘Pass’, :y =\> pass},  
{:name =\> ‘Fail’, :y =\> fail}  
]  
pie\_label\_formatter = ’  
function() {  
if (this.y \> 15) return this.point.name;  
}’

pie\_tooltip\_formatter = ’  
function() {  
return “ **” + this.point.name + “** : " + this.y + " %”;  
}’

@pie\_chart =  
Highchart.pie({  
:chart =\> {  
:renderTo =\> “pie-chart-container”,  
:margin =\> [50, 30, 0, 30]  
},  
:plotOptions =\> {  
:pie =\> {  
:dataLabels =\> {  
:formatter =\> pie\_label\_formatter,  
:style =\> {  
:textShadow =\> ‘#000000 1px 1px 2px’  
}  
}  
}  
},  
:series =\> [  
{  
:type =\> ‘pie’,  
:data =\> student\_data  
}  
],  
:subtitle =\> {  
:text =\> ‘April 2010’  
},  
:title =\> {  
:text =\> ‘Student Status Chart’  
},  
:tooltip =\> {  
:formatter =\> pie\_tooltip\_formatter  
},  
})

Note if I put this: :data =\> student\_data.to\_json It actually returns a  
json string of my query as text in the browser. Also, if I hard code  
values (e.g. :y =\> 1), it will render the chart properly. However, any  
database query will not render the chart properly. So I’m not sure  
exactly what the issue is. Any suggestions? Thanks.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [April 2, 2010, 11:37am UTC](https://www.ruby-forum.com/t/ruby-array-javascript-and-json-issue/186198/2 "2010-04-02T11:37:26Z")

</div>

On Apr 1, 11:29 pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> pass = Student.find\_by\_sql(‘SELECT COUNT(_) FROM students WHERE  
> student\_state = 1’)  
> fail = Student.find\_by\_sql('SELECT COUNT(_) FROM students WHERE  
> student\_state = 2’)

Are you expecting pass and fail to be numbers? They’re not -  
Student.find\_by\_sql always returns an array of student objects. Sounds  
like you might want count.

Fred
