Rails - Project.where(created_at:(Time.now - 24.hours).. Time.now)返回<project :: activerecord_relation:0x007f5470c7a378>

时间:2015-10-31 15:03:08

标签: ruby-on-rails

我想要做的是获取过去24小时内发布的所有项目的数量。

class ApplicationController < ActionController::Base

  before_action :projects_today

  def projects_today
    @projects_today = Project.where(created_at: (Time.now - 24.hours)..Time.now)
  end

end

查看

<h1><%= @projects_today %></h1>

然而,在视图中出现的是&#34;项目:: ActiveRecord_Relation:0x007f5470c7a378 &#34;而不是62,这是期望的答案。

1 个答案:

答案 0 :(得分:1)

您忘记添加count

@projects_today = Project.where(created_at: (Time.now - 24.hours)..Time.now).count