更新时间:2022-04-26 21:57:31
您的 total_on
方法返回的是关联,而不是数字.
Your total_on
method is returning an association, not a number.
尝试附加 .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
更新:
原始的placed_at"查询是否有效,还是因为NoMethodError
而从未真正调用过?
Did the original "placed_at" query ever work, or was it never actually called because of the NoMethodError
?
您的模型中似乎没有 placed_at
列,但我假设您也可以使用 created_at
.
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.
如果你想使用placed_at
,你可以定义这个方法(为了样式我把名字改成了placed_on
):
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
您可以将其链接到您的 total_on
方法中:
You can chain this into your total_on
method:
class Phone < ActiveRecord::Base
def total_on(date)
calls.placed_on(date).count
end
end