我有一个rails应用程序,我创建了一个高图表线图,以比较2个行业的股票,电信和农业,同时跟踪这,瑞安贝茨高股票视频。
该图形将在视图( index.html.erb of stock_quotes )中输出,其中高级图表的javascipt代码如下所示:
<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>stock_quote.rb模型如下:
scope :agric, where(category: "Agriculture")
scope :telecomm, where(category: "Telecommunication")
def self.price_on(date)
where("date(published_at) = ?", date).sum(:total_price)
end这就是在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] }, ] }); }); 发布于 2014-05-06 08:54:01
相反,请看rails 4作用域语法:
scope :agric, where(category: "Agriculture")
scope :telecomm, where(category: "Telecommunication")用途:
scope :agric, -> { where(category: "Agriculture") }
scope :telecomm, -> { where(category: "Telecommunication") }发布于 2014-05-06 08:54:40
范围定义必须使用proc对象,而不是普通where语句。这是因为它们需要在对作用域的实际查询中进行评估,而不是在类的初始设置期间。因此,请在您的模型中使用此方法:
scope :agric, -> { where(category: "Agriculture") }
scope :telecomm, -> { where(category: "Telecommunication") }https://stackoverflow.com/questions/23490081
复制相似问题