我正在尝试显示事件调用和待处理调用总数的数值。目前,我将这些分成两个面板,并希望包括一个可见的数字以显示每种类型中有多少。
我已经搜索过了,但它非常复杂,没有多大意义。
列名为“status”,在调用创建页面中可以选择“assigned”和“pending”
下面是我的 Controller 供引用。
调用 Controller :
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 方面是我的弱点,我目前正在努力加强这一点。
预先感谢您的帮助。
最佳答案
取决于您希望在何处(哪个 Controller 操作)执行计数。
如果在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
。原因是每次访问数据库都会带来开销。它可能看起来并不多,但这些延迟加起来会使您的 Controller Action 变慢。所以通常你想尽可能地减少访问数据库的次数。上面的代码使用 Ruby 的 select
方法遍历数组并选择 block 被评估为 true 的元素;这发生在内存中,不需要访问数据库。
如果它在不同的 Controller 操作中:
假设在其他 Controller 操作中您没有加载 Call
Active Record 对象数组,您可能想要发出 COUNT(*)
查询。这些通常比加载所有记录并在内存中执行计数(例如使用 length
)更快,因为数据库服务器只需要返回一个数字而不是所有记录数据,并且您还保存构建 Active Record 对象的开销。
使用 Active Record 的方法很简单:
Call.where(status: 'active').count
Call.where(status: 'pending').count
如果您想使用 GROUP BY
执行一个 COUNT
查询而不是两个单独的查询(好主意,因为它可以节省额外的数据库访问),你可以这样做:
Call.group(:status).count
这会产生一个散列,例如 {'active' => 150, 'pending' => 120}
,您可以在您的 View 中使用它。
关于html - 如何根据属性显示Postgresql DB中的总记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642387/