#<ActiveRecord::Relation:0x472d0a0>的未定义方法`to_f' [英] undefined method `to_f' for #<ActiveRecord::Relation:0x472d0a0>
问题描述
我正在尝试构建一个呼叫跟踪应用程序,作为学习 Rails 和 twilio 的一种方式.现在,我想做的是制作一个图表,显示特定电话每天接到的电话数量.
I'm trying to build a call tracking app as a way to learn rails and twilio. Right now, what I'd like to do is to make a graph that shows the number of phone calls a particular phone gets every day.
我的数据结构的设置方式是电话模型有_many 调用.
The way my data structure is set up, is that the phone model has_many calls.
在我的手机模型中,我有
In my Phone Model, I have
def total_on(date)
self.calls.where("placed_at >= ? AND placed_at <= ?", date.beginning_of_day, date.end_of_day)
end
在我看来,我有以下 javascript :
In my view, I have the following javascript :
<script type="text/javascript" charset="utf-8">
$(function () {
new Highcharts.Chart({
chart: { renderTo: 'calls_chart' },
title: { text: 'Calls Per Day' },
xAxis: { type: 'datetime' },
yAxis: {
title: { text: ' Calls'}
},
series: [{
name: "<%= @phone.name %>",
pointInterval: <%= 6.days * 1000 %>,
pointStart: <%= 1.month.ago.at_midnight.to_i * 1000 %>,
data: <%= (1.month.ago.to_date..Date.today).map { |date| @phone.total_on(date).to_f}.inspect %>
}]
});
});
重要的部分是数据:
<%= (1.month.ago.to_date..Date.today).map { |date| @phone.total_on(date).to_f}.inspect %>
当我尝试每天放置呼叫数据时,出现错误
when I try to place the data for calls on each day, I get the error
undefined method `to_f' for #<ActiveRecord::Relation:0x472d0a0>
老实说,我不确定我是否在这里做任何事情.stackoverflower 的一位同事帮助我制作了上面引用的实例方法,所以我认为这比我写的要好得多.
I'm not really sure if I'm doing anything right here, to be honest. A fellow stackoverflower helped me make the Instance Method I referenced above, so I'm assuming that's a lot better than what I wrote.
那么,我需要更改哪些内容才能在视图中拨打特定日期的电话号码?
So, what would I need to change to make it so that I can call the number of phone calls made on a particular date, in the view?
更新
在 Zack 的回答之后,我现在收到错误
After Zack's answer, I now get the error
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')
可能我混淆了我的联想或其他东西.这是具有数据结构的模型,如果它们有帮助的话 --
Probably I'm confusing the associations I have or something. Here are the models with the data structure, if they help --
手机型号
# == 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 < ActiveRecord::Base
attr_accessible :original_number, :user_id, :name, :twilio_number
belongs_to :user
has_many :calls, dependent: :destroy
validates :name, presence: true
validates :twilio_number, presence: true
validates :original_number, presence: true
validates :user_id, presence: true
default_scope order: 'phones.created_at DESC'
validate :check_phone_limit, :on => :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 < ActiveRecord::Base
attr_accessible :AccountSid, :From, :To, :CallStatus, :ApiVersion, :Direction, :FromCity, :FromState, :FromZip, :FromCountry, :ToCity, :ToState, :ToZip, :ToCountry, :CallSid, :DialCallSid, :DialCallDuration, :DialCallStatus, :RecordingUrl, :DialCallMinutes
belongs_to :phone
def self.create_from_incoming_call(params)
user_phone = Phone.find_by_twilio_number(params['To']) #Finds the phone number in the database based on what phone Twilio is calling
twilio_request_params = {
:CallSid => 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(为了便于阅读而重复)
And, the javascript in the view ( repeated for easier reading)
<script type="text/javascript" charset="utf-8">
$(function () {
new Highcharts.Chart({
chart: { renderTo: 'calls_chart' },
title: { text: 'Calls Per Day' },
xAxis: { type: 'datetime' },
yAxis: {
title: { text: ' Calls'}
},
series: [{
name: "<%= @phone.name %>",
pointInterval: <%= 6.days * 1000 %>,
pointStart: <%= 1.month.ago.at_midnight.to_i * 1000 %>,
data: <%= (1.month.ago.to_date..Date.today).map { |date| @phone.total_on(date).to_f}.inspect %>
}]
});
});
</script
>
推荐答案
您的 total_on
方法返回的是关联,而不是数字.
Your total_on
method is returning an association, not a number.
尝试附加 .count
:
def total_on(date)
calls.where("placed_at >= ? AND placed_at <= ?", date.beginning_of_day, date.end_of_day).count
end
你也可以给它一个日期范围来稍微缩短这个方法(实际上是一样的,但更容易阅读):
You can also give it a date range to shorten the method a bit (effectively the same, but easier to read):
def total_on(date)
calls.where(placed_at: date.beginning_of_day..date.end_of_day).count
end
更新:
原始的placed_at"查询是否有效,还是因为NoMethodError
而从未真正调用过?
Did the original "placed_at" query ever work, or was it never actually called because of the NoMethodError
?
您的模型中似乎没有 placed_at
列,但我假设您也可以使用 created_at
.
It doesn't look like there is a placed_at
column in your model, but I assume you can used created_at
just as well.
如果你想使用placed_at
,你可以定义这个方法(为了样式我把名字改成了placed_on
):
If you want to used placed_at
, you could define this method (I changed the name to placed_on
for style):
class Call < ActiveRecord::Base
def self.placed_on(date)
where(created_at: date.beginning_of_day..date.end_of_day)
end
end
您可以将其链接到您的 total_on
方法中:
You can chain this into your total_on
method:
class Phone < ActiveRecord::Base
def total_on(date)
calls.placed_on(date).count
end
end
这篇关于#<ActiveRecord::Relation:0x472d0a0>的未定义方法`to_f'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!