1

I have a rails app whereby I'm creating a highcharts line graph to compare stocks in 2 industries, Telecommunication and Agriculture while following this, ryan bates highstocks video.

The graph will be output in a view, index.html.erb of stock_quotes whereby the javascipt code for highcharts is as follows:

<div id="quotes_chart", style="width=560px; height:300px;">

  $(function(){   
  new HighCharts.Chart( 
  chart: {
    renderTo: "quotes_chart"
  },
  title: {
   text: "Daily trades" 
  },
  xAxis: {
   type: "datetime"
  },
  yAxis: {
   title: {
     text: "Shillings"
  }
},
tooltip: {
  formatter: function(){
    return HighCharts.dateFormat("%B %e, %Y", this.x) + ': ' + "Kshs" + Highcharts.numberFormat(this.y, 2);
  }
},
series: [
<% { "Telecommunication" => StockQuote.telecomm, "Agriculture" => StockQuote.agric }.each do |name, prices|
%>
{
  name: <%= name %>
  pointInterval: <%= 1.day * 1000 %>,
  pointStart: <%= 2.weeks.ago.to_i * 1000 %>,
  data: <%= (2.weeks.ago.to_date..Date.today).map { |date| StockQuote.price_on(date).to_f}.inspect%>
},
<% end %>]
  }); 
}); 
 </div>

The model stock_quote.rb is as follows:

scope :agric, where(category: "Agriculture")
scope :telecomm, where(category: "Telecommunication")

def self.price_on(date)
  where("date(published_at) = ?", date).sum(:total_price)
end

This is what is being output on the stock_quotes div:

$(function(){ new HighCharts.Chart( chart: { renderTo: "quotes_chart" }, title: { text: "Daily trades" }, xAxis: { type: "datetime" }, yAxis: { title: { text: "Shillings" } }, tooltip: { formatter: function(){ return HighCharts.dateFormat("%B %e, %Y", this.x) + ': ' + "Kshs" + Highcharts.numberFormat(this.y, 2); } }, series: [ { name: Telecommunication pointInterval: 86400000, pointStart: 1398157330000, data: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 554.0] }, { name: Agriculture pointInterval: 86400000, pointStart: 1398157330000, data: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 554.0] }, ] }); }); 
Mutuma
  • 1,943
  • 2
  • 22
  • 33

2 Answers2

3

Look rails 4 scope syntax instead:

  scope :agric, where(category: "Agriculture")
  scope :telecomm, where(category: "Telecommunication")

use:

  scope :agric, -> { where(category: "Agriculture") }
  scope :telecomm, -> { where(category: "Telecommunication") }
Community
  • 1
  • 1
Philidor
  • 40,933
  • 9
  • 84
  • 94
  • every day the same questions. – Philidor May 06 '14 at 08:55
  • Solved the syntax error, but check my comment on Holger Just's answer below – Mutuma May 06 '14 at 09:01
  • The div was closed its only I had not included it, I've edited the code though – Mutuma May 06 '14 at 09:11
  • My first assumption is that I had not installed jquery-rails gem properly, but I then went ahead and reinstalled it in my app follwoing [this](http://buddylindsey.com/add-jquery-to-ruby-on-rails/) blog and it still doesn't show anything – Mutuma May 06 '14 at 09:18
  • try [highcharts gem](https://github.com/PerfectlyNormal/highcharts-rails) for rails 4. – Philidor May 06 '14 at 09:22
0

Your scope definitions must be with a proc object, not a plain where statement. This is because they need to be evaluated during the actual query of the scope, not during the initial setup of the class. As such, please use this in your model:

scope :agric, -> { where(category: "Agriculture") }
scope :telecomm, -> { where(category: "Telecommunication") }
Holger Just
  • 49,152
  • 14
  • 106
  • 117
  • Yeah...I did that and there is no error, However instead of my javascript graph being output, I'm getting my javascript code being shown on that div. – Mutuma May 06 '14 at 08:59