1

可能重复:
#<ActiveRecord::Relation:0x472d0a0> 的未定义方法 `to_f'

我正在尝试制作一个呼叫跟踪应用程序来学习 twilio 和 rails。

现在,我想制作一个图表,向用户显示特定电话号码每天接到多少电话。

架构是用户 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

我已经坚持了一段时间了;任何帮助将不胜感激!

4

1 回答 1

2

您的调用模型使用标准 rails created_at,但您的查询使用的是不存在的放置的_at。

于 2012-10-15T19:22:49.160 回答