#MedRecord :: Relation:0x472d0a0>的未定义方法`to_f’

我正在尝试构建一个呼叫跟踪应用程序,作为学习rails和twilio的一种方式。 现在,我想做的是制作一个图表,显示每天特定手机的电话数量。

我的数据结构的设置方式是手机型号有多次调用。

在我的手机型号中,我有

def total_on(date) self.calls.where("placed_at >= ? AND placed_at <= ?", date.beginning_of_day, date.end_of_day) end 

在我看来,我有以下javascript:

  $(function () { new Highcharts.Chart({ chart: { renderTo: 'calls_chart' }, title: { text: 'Calls Per Day' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: ' Calls'} }, series: [{ name: "", pointInterval: , pointStart: , data:  }] }); }); 

重要的部分是数据:

  

当我尝试每天拨打电话数据时,我收到错误

 undefined method `to_f' for # 

老实说,我不确定我是否在这里做任何事情。 一个伙伴stackoverflower帮我制作了上面提到的实例方法,所以我假设它比我写的要好很多。

那么,我需要改变什么才能在视图中拨打特定日期的电话号码?

UPDATE

在Zack回答之后,我现在得到了错误

 SQLite3::SQLException: no such column: calls.placed_at: SELECT COUNT(*) FROM "calls" WHERE "calls"."phone_id" = 44 AND ("calls"."placed_at" BETWEEN '2012-09-11 00:00:00.000000' AND '2012-09-11 23:59:59.999999') 

可能我混淆了我所拥有的关联或其他东西。 以下是具有数据结构的模型,如果它们有帮助 –

手机型号

 # == Schema Information # # Table name: phones # # id :integer not null, primary key # name :string(255) # twilio_number :integer # original_number :integer # user_id :integer # created_at :datetime not null # updated_at :datetime not null # class Phone  :create def check_phone_limit if User.find(self.user_id).at_max_phone_limit? self.errors[:base] << "Cannot add any more phones" end end def original_number=(value) num = value.to_s.gsub(/[^0-9+]/, "") write_attribute(:original_number, num.to_i) end def total_on(date) self.calls.where(placed_at: date.beginning_of_day..date.end_of_day).count end end 

呼叫模型

 # == Schema Information # # Table name: calls # # id :integer not null, primary key # AccountSid :string(255) # From :string(255) # To :string(255) # CallStatus :string(255) # ApiVersion :string(255) # Direction :string(255) # FromCity :string(255) # FromState :string(255) # FromZip :string(255) # FromCountry :string(255) # ToCity :string(255) # ToState :string(255) # ToZip :string(255) # ToCountry :string(255) # CallSid :string(255) # DialCallSid :string(255) # DialCallDuration :string(255) # DialCallStatus :string(255) # RecordingUrl :string(255) # phone_id :integer # DialCallMinutes :integer # created_at :datetime # updated_at :datetime # class Call  params['CallSid'], :AccountSid => params['AccountSid'], :From => params['From'], :To => params['To'], :CallStatus => params['CallStatus'], :ApiVersion => params['ApiVersion'], :Direction => params['Direction'], :FromCity => params['FromCity'], :FromState => params['FromState'], :FromZip => params['FromZip'], :FromCountry => params['FromCountry'], :ToCity => params['ToCity'], :ToState => params['ToState'], :ToZip => params['ToZip'], :ToCountry => params['ToCountry'] } call = Call.new(twilio_request_params) call.save return call end def Call.update_dial_call(params) twilio_request_params = { :DialCallSid => params['DialCallSid'], :DialCallDuration => params['DialCallDuration'], :DialCallStatus => params['DialCallStatus'], :RecordingUrl => params['RecordingUrl'], :DialCallMinutes => (params['DialCallDuration'].to_f/60.to_f).ceil } call = Call.where( :CallSid => params['CallSid'] ).first call.update_attributes twilio_request_params call.save end end 

电话显示行动

  def show @user = current_user @phone = Phone.find_by_id(params[:id]) @calls = @phone.calls.paginate(page: params[:page]) end 

并且,视图中的javascript(重复以便于阅读)

  $(function () { new Highcharts.Chart({ chart: { renderTo: 'calls_chart' }, title: { text: 'Calls Per Day' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: ' Calls'} }, series: [{ name: "", pointInterval: , pointStart: , data:  }] }); }); </script 

>

你的total_on方法返回一个关联,而不是一个数字。

尝试附加.count

 def total_on(date) calls.where("placed_at >= ? AND placed_at <= ?", date.beginning_of_day, date.end_of_day).count end 

你也可以给它一个日期范围来缩短方法(实际上相同,但更容易阅读):

 def total_on(date) calls.where(placed_at: date.beginning_of_day..date.end_of_day).count end 

更新:

原始的“placement_at”查询是否有效,或者由于NoMethodError而从未实际调用NoMethodError

看起来你的模型中没有一个placed_at列,但我认为你也可以使用created_at

如果你想使用placed_at ,你可以定义这个方法(我将名称placed_on为了样式的placed_on ):

 class Call < ActiveRecord::Base def self.placed_on(date) where(created_at: date.beginning_of_day..date.end_of_day) end end 

您可以将其链接到total_on方法:

 class Phone < ActiveRecord::Base def total_on(date) calls.placed_on(date).count end end