2012-10-11 124 views
0

我試圖構建一個通話跟蹤應用程序,作爲學習rails和twilio的一種方式。現在,我想要做的是製作一張圖表,顯示每天特定電話撥打的電話數量。未定義的方法`to_f'爲#<ActiveRecord :: Relation:0x472d0a0>

我的數據結構設置的方式是手機型號has_many調用。

在我的手機型號,我有

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

在我看來,我有以下的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 %> 

時我嘗試在每天的撥打電話的數據,我得到錯誤

undefined method `to_f' for #<ActiveRecord::Relation:0x472d0a0> 

我真的不確定我是否在這裏做任何事情,說實話。同伴stackoverflower幫助我製作上面引用的實例方法,所以我認爲這比我寫的要好得多。

那麼,我需要做些什麼才能使它能夠在視圖中調用在特定日期發出的電話號碼?

UPDATE

扎克的回答後,我現在得到的錯誤

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 < 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的在視圖中(重複以便於閱讀)

<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 

>

+0

我對Rails提供的語法答案不夠熟悉,但注意'total_on(date)'的返回似乎是電話呼叫的***子集*,而不是呼叫的*號碼* 。在將其轉換爲數字之前,您需要公開該子集中的通話計數。 –

+0

你有一個ActiveRecord對象。如果你這樣做會發生什麼:'@ phone.total_on(date)[0] .to_f – thisfeller

+0

你好,我得到錯誤:未定義的局部變量或方法'日期'爲#<#:0x465f108>。我將用ActiveRecord對象更新這個問題,並且我會給出一些關於當我刪除to_f –

回答

1

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 

更新:

原始的「置位_at」查詢是否曾經工作,或者是否因爲而從未實際調用過3210?

看起來您的模型中沒有placed_at列,但我認爲您也可以使用created_at

如果你想使用placed_at,你可以定義這個方法(我的名字改爲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 
+0

時會發生什麼的問題,謝謝你的快速回答。我認爲你的答案是正確的,但我認爲我沒有正確地調用正確的列。我現在得到錯誤: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'和'2012-09-11 23:59:59.999999')。我會用我的手機型號,調用模型和activerecord對象來更新問題。我會喜歡你的想法 –

相關問題