对于明确存在的记录,数据库返回nil

时间:2017-04-18 21:21:27

标签: ruby-on-rails

我有这个控制器,我很快就把两个电话号码的来电联系起来。我构建了应用程序并使用ngrok测试了代码,一切正常。我把它上传到我的数字海洋水滴,现在的东西不能像我们当地环境那样工作。我正在得到一个显然是由于设置为nil而导致的错误,我认为我的代码占了我的代码。无论如何这里是代码和触发它的错误。我将首先提出问题的代码,然后是我的整个控制器文件后面的错误消息。行私有在213行我将opNum方法私有,所以我可以从同一个控制器中的其他方法调用它

private
def opNum
aviOperators = Operator.where(working: true).find_each
list = []
aviOperators.each do |o|
  list.push(o.calls.last)
end
list.sort! {|a, b| a.created_at <=> b.created_at}
if list.empty?
  false
else
  Operator.find(list.first.operator_id).phoneO
end
end

I,[2017-04-18T16:43:30.226384#2019]信息 - :在215ms内完成500内部服务器错误(ActiveRecord:10.3ms) F,[2017-04-18T16:43:30.229647#2019]致命 - : NoMethodError(opNum中未定义的方法created_at' for nil:NilClass): app/controllers/voice_controller.rb:220:in块'   app / controllers / voice_controller.rb:220:sort!' app/controllers/voice_controller.rb:220:in opNum'   app / controllers / voice_controller.rb:69:在`findOp'

require 'twilio-ruby'
class VoiceController < ApplicationController
  include Webhookable

  after_filter :set_header

  skip_before_action :verify_authenticity_token

  URL = "http://botnc.com"

  def recive
    num = params['From']
    sid = params['CallSid']
    Call.create(:phoneC => num, :callSid => sid)

      response = Twilio::TwiML::Response.new do |r|
      r.Gather :numDigits => '1', :action => "/voice/findOp", :method => 'post', :timeout => 5 do |g|
        g.Play "#{URL}/assets/mp1.mp3"
                end
              end

            render_twiml response
  end

  def lang
    sid = params['CallSid']
    lang = params['Digits']
    call = Call.find_by_callSid( sid )  ##find_each might search through the whole db. try and make it so the DB is searched from the end towards the front
    id = call.read_attribute('id')
    Call.find(id).update(lang: lang)
   response = Twilio::TwiML::Response.new do |r|
       r.Enqueue "Wait"
     end

            render_twiml response

  end

  def retrive
    if opNum == false
     response =Twilio::TwiML::Response.new do |r|
                  r.Play "#{URL}/assets/waitOp.mp3"
                  end

                render_twiml response
      else
              response =Twilio::TwiML::Response.new do |r|
                r.Leave
                        end
                      render_twiml response
    end


  end

  def findOp
    sid = params['CallSid']
    lang = params['Digits']
    call = Call.find_by_callSid( sid )  ##find_each might search through the whole db. try and make it so the DB is searched from the end towards the front
    id = call.read_attribute('id')
    Call.find(id).update(lang: lang)
      #aviOperators = Operator.where(working: true).find_each
      #list = []
      #aviOperators.each do |o|
        #list.push(o.calls.last)
        #end
      #list.sort! {|a, b| a.created_at <=> b.created_at}

      if opNum == false
        response = Twilio::TwiML::Response.new do |r|
          r.Enqueue "Wait", waitUrl: "#{URL}/voice/retrive/"
          r.Redirect "#{URL}/voice/polishCall/"
          #r.Gather :numDigits => '1', :action => '#{URL}/assets/wait.xml', :method => 'get', :timeout => 8 do |g|
            #g.Say "Press one to hold for the next represenative"
          end 

          #r.Dial '+1'+@operators
          #end
          render_twiml response
      else
      response = Twilio::TwiML::Response.new do |r|  
     r.Redirect "#{URL}/voice/polishCall/"
       end
       render_twiml response
      end
  end

  def collectCard
    cardId = params["Digits"]
    @card = Card.find_by_pinNum(cardId)
    value = 0.0
    if @card == nil
      response = Twilio::TwiML::Response.new do |r|  
          r.Play "#{URL}/assets/invalidCard.mp3"
           r.Play "#{URL}/assets/goodbye.mp3"
       end
       render_twiml response
    else
    value = @card.cValue.to_f
    if value > 0.0
      sid = params["CallSid"]
      @call = Call.find_by_callSid(sid)
      id = @card.id
      @call.update(card_id: id)
      value.to_s
      response = Twilio::TwiML::Response.new do |r|  
          r.Say  "Tienes #{value} dolares en tu tarjeta", :language  => "es"
           r.Play "#{URL}/assets/waitOp.mp3"
           r.Dial opNum, :action => "#{URL}/voice/finish/"  #This is where you need to put the action url to update the call with duration and the card with consumption
       end
       render_twiml response
    else
      response = Twilio::TwiML::Response.new do |r|  
          r.Play "#{URL}/assets/noMinutes.mp3"
          r.Play "#{URL}/assets/goodbye.mp3"
           #r.Play "#{URL}/assets/goodbye.mp3"
       end
       render_twiml response
     end
    end



  end
  def finish
    duration = params["DialCallDuration"].to_i / 60 + 1
    sid = params["CallSid"]
    @call = Call.find_by_callSid(sid)
    @call.update(fDuration: duration )
    @card = Card.find_by_id(@call.card_id)
    newvalue = @card.cValue - (duration * 0.5)
    @card.update(cValue: newvalue)
    response = Twilio::TwiML::Response.new do |r|
      r.Say "Tienes #{newvalue} dinero pendiente", :language => "es" 
      end
    render_twiml response



  end

  def sales
    if Operator.find(1).working
      response = Twilio::TwiML::Response.new do |r|
        r.Dial "+19197634923"
        end
      render_twiml response
    else
      response = Twilio::TwiML::Response.new do |r|
        r.Say "Lo siento por favor llame a lunes entre 8 y 5", :language => "es" 
        r.Play "#{URL}/assets/goodbye.mp3"#
      end
      render_twiml response
  end
end
  def polishCall
    sid = params["CallSid"]
    lang = Call.find_by_callSid(sid).lang
    numOp = opNum
    opId = Operator.find_by_phoneO(opNum).id
    Call.find_by_callSid(sid).update(operator_id: opId)
    if lang == "2"
    response = Twilio::TwiML::Response.new do |r|
      r.Dial "+1#{numOp}" #this may be causing Problem
            end

            render_twiml response
        elsif lang == "1"
          response = Twilio::TwiML::Response.new do |r|
            r.Gather :numDigits => '1', :action => "#{URL}/voice/menue/", :method => 'post', :timeout => 10 do |g|
              g.Play "#{URL}/assets/menue.mp3"#the menue Audio audio
            end
          end
          render_twiml response
            else
              response = Twilio::TwiML::Response.new do |r|
                r.Say "You did'nt let use know what language you speak"
                    end
                    render_twiml response
                  end
  end
  def menue
    menue = params["Digits"]
    if menue == "1"
      response = Twilio::TwiML::Response.new do |r|
        r.Gather :numDigits => '7', :action => "#{URL}/voice/collectCard/", :method => 'post', :timeout => 10 do |g|
          g.Play "#{URL}/assets/CaptureCard.mp3"#the menue Audio audio
        end
      end
      render_twiml response
    elsif menue == "2"
      response = Twilio::TwiML::Response.new do |r|
        r.Gather :numDigits => '1', :action => "#{URL}/voice/sales/", :method => 'post', :timeout => 10 do |g|
          g.Play "#{URL}/assets/services.mp3"#the Services itemes and phone number for sales
        end
      end
      render_twiml response
    elsif menue == "3"
      response = Twilio::TwiML::Response.new do |r|
          r.Play "#{URL}/assets/storeList.mp3"
          r.Play "#{URL}/assets/goodbye.mp3"
        end
        render_twiml response
    else
      response = Twilio::TwiML::Response.new do |r|
          r.Play "#{URL}/assets/goodbye.mp3"
        end
        render_twiml response
    end
  end


  private
  def opNum
    aviOperators = Operator.where(working: true).find_each
    list = []
    aviOperators.each do |o|
      list.push(o.calls.last)
    end
    list.sort! {|a, b| a.created_at <=> b.created_at}
    if list.empty?
      false
    else
      Operator.find(list.first.operator_id).phoneO
    end
  end


end

1 个答案:

答案 0 :(得分:0)

好的想通了。如果你无法解决这个问题,请稍后再回过头来,答案就在那里。所以问题是我的opNum方法查看了我的每个运算符以及从之前的实例分配给它们的调用。好吧,因为它是一个新的数据库。我的所有操作员都没有电话归因于他们,这就是为什么我在创建的查询中获得nil的原因。用一些调用和bam作品播种数据库