undefined method `to_f' for #

前端 未结 1 1104
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 07:30

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 partic

相关标签:
1条回答
  • 2020-12-22 08:06

    Your total_on method is returning an association, not a number.

    Try appending .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
    

    Update:

    Did the original "placed_at" query ever work, or was it never actually called because of the NoMethodError?

    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.

    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
    

    You can chain this into your total_on method:

    class Phone < ActiveRecord::Base
    
      def total_on(date)
        calls.placed_on(date).count
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题