【问题标题】:how to display total records in Postgresql DB based on attribute如何根据属性显示 Postgresql DB 中的总记录
【发布时间】:2015-11-11 00:45:30
【问题描述】:

我正在尝试显示活动和待处理呼叫总数的数值。目前我将这些分成两个面板,并希望包括一个可见的数字来显示每种类型中有多少在 que 中。

我已经对此进行了搜索,但它非常复杂,没有多大意义。

列名是“status”,在通话创建页面中可以选择“assigned”和“pending”

以下是我的控制器供参考。

调用控制器:

class CallsController < ApplicationController
  before_action :set_call, only: [:show, :edit, :update, :destroy]

  # GET /calls
  # GET /calls.json
  def index
    @calls = Call.all
    @active_calls = Call.where(status: 'active')
    @pending_calls = Call.where(status: 'pending')
  end

  # GET /calls/1
  # GET /calls/1.json
  def show
  end

  # GET /calls/new
  def new
    @call = Call.new
  end

  # GET /calls/1/edit
  def edit
  end

  # POST /calls
  # POST /calls.json
  def create
    @call = Call.new(call_params)

    respond_to do |format|
      if @call.save
        format.html { redirect_to @call, notice: 'Call was successfully created.' }
        format.json { render :show, status: :created, location: @call }
      else
        format.html { render :new }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /calls/1
  # PATCH/PUT /calls/1.json
  def update
    respond_to do |format|
      if @call.update(call_params)
        format.html { redirect_to @call, notice: 'Call was successfully updated.' }
        format.json { render :show, status: :ok, location: @call }
      else
        format.html { render :edit }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /calls/1
  # DELETE /calls/1.json
  def destroy
    @call.destroy
    respond_to do |format|
      format.html { redirect_to calls_url, notice: 'Call was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_call
      @call = Call.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def call_params
      params.require(:call).permit(:call_time, :status, :primary_type, :secondary_type, :site, :address, :unit_1, :unit_2, :unit_3, :unit_4, :call_details, :unit_on_scene, :unit_clear, :call_num, :site_id)
    end
end

Postgresql 方面是我的弱点,我目前正在努力加强这一点。

提前感谢您的帮助。

【问题讨论】:

    标签: html css ruby postgresql ruby-on-rails-4


    【解决方案1】:

    取决于您要执行计数的位置(哪个控制器操作)。

    如果在index

    您似乎已经将相关记录加载到内存中:

    @active_calls = Call.where(status: 'active')
    @pending_calls = Call.where(status: 'pending')
    

    所以你不需要发出新的查询,你可以像这样简单地调用 Ruby 的 length 方法:

    @active_calls.length
    @pending_calls.length
    

    顺便说一句,我可能会避免像你现在那样运行三个查询,而是做这样的事情:

    @calls = Call.all 
    @active_calls = @calls.select{|x| x.status == 'active'}
    @pending_calls = @calls.select{|x| x.status == 'pending'}
    

    并像以前一样使用@active_calls.length@pending_calls.length。原因是每次访问数据库都会产生开销。看起来可能不多,但这些延迟加起来会使您的控制器动作变慢。因此,通常您希望尽可能减少访问数据库的次数。上面的代码使用 Ruby 的 select 方法遍历数组并选择块被评估为真的元素;这发生在内存中,不需要访问数据库。

    如果它在不同的控制器操作中:

    假设在其他控制器操作中您没有加载Call Active Record 对象的数组,您可能想要发出COUNT(*) 查询。这些通常比加载所有记录并在内存中执行计数更快(例如使用length),因为数据库服务器只需要返回一个数字而不是所有记录数据,并且您还可以节省构建 Active 的开销记录对象。

    使用 Active Record 的方法很简单:

    Call.where(status: 'active').count
    Call.where(status: 'pending').count
    

    如果您想用GROUP BY 执行一个COUNT 查询而不是两个单独的查询(好主意,因为它可以节省额外的数据库访问),您可以这样做:

    Call.group(:status).count
    

    这将产生一个哈希值,例如 {'active' =&gt; 150, 'pending' =&gt; 120},您可以在视图中使用它。

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2018-07-24
      • 1970-01-01
      相关资源
      最近更新 更多