2012-10-15 83 views
1

可能重複:
undefined method `to_f' for #<ActiveRecord::Relation:0x472d0a0>的SQLite3 ::的SQLException:沒有這樣的列:

我試圖撥打電話跟蹤應用學習twilio和護欄。

現在,我想製作一張圖表,向用戶顯示每天特定電話號碼撥打多少電話。

模式是用戶has_many手機has_many調用。

我試圖通過創建計算某一天的手機數量的實例方法,使圖形,但是當我試圖執行的代碼,我得到的錯誤:

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

我不非常理解我用於實例方法的代碼,它可能調用了錯誤的列。你的幫助將不勝感激。

這裏是我的呼叫模型的重要組成部分:

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

這裏是我如何在我的節目視圖計數的電話

<%= (1.month.ago.to_date..Date.today).map { |date| @phone.total_on(date).to_f}.inspect %> 

下面是我定義@phone變量

@phone = Phone.find_by_id(params[:id]) 

這是我的完整手機型號(用於架構參考)

# == 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) 
    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'] 
     :phone_id => user_phone.phone_id 

    } 


    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 

我一直停留在這一段時間;任何幫助將不勝感激!

+0

我看不到您的調用表中的位置列,你確定你不是指的created_at? – Doon

+0

...哇。 Doon,謝謝。請把它作爲答案,我會尷尬地接受:P。 –

回答

2

您的調用模型使用標準rails created_at,但您的查詢使用了不存在的placed_at。

相關問題