首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 4.0未定义方法“调用”

Rails 4.0未定义方法“调用”
EN

Stack Overflow用户
提问于 2014-05-06 08:51:57
回答 2查看 301关注 0票数 1

我有一个rails应用程序,我创建了一个高图表线图,以比较2个行业的股票,电信和农业,同时跟踪,瑞安贝茨高股票视频。

该图形将在视图( index.html.erb of stock_quotes )中输出,其中高级图表的javascipt代码如下所示:

代码语言:javascript
复制
<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模型如下:

代码语言:javascript
复制
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上输出的内容:

代码语言:javascript
复制
$(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] }, ] }); }); 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-06 08:54:01

相反,请看rails 4作用域语法

代码语言:javascript
复制
  scope :agric, where(category: "Agriculture")
  scope :telecomm, where(category: "Telecommunication")

用途:

代码语言:javascript
复制
  scope :agric, -> { where(category: "Agriculture") }
  scope :telecomm, -> { where(category: "Telecommunication") }
票数 3
EN

Stack Overflow用户

发布于 2014-05-06 08:54:40

范围定义必须使用proc对象,而不是普通where语句。这是因为它们需要在对作用域的实际查询中进行评估,而不是在类的初始设置期间。因此,请在您的模型中使用此方法:

代码语言:javascript
复制
scope :agric, -> { where(category: "Agriculture") }
scope :telecomm, -> { where(category: "Telecommunication") }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23490081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档