gpt4 book ai didi

Ruby-on-rails Postgres 查询

转载 作者:行者123 更新时间:2023-11-29 13:43:48 25 4
gpt4 key购买 nike

我有一个用于酒店的 ruby​​ on rails 应用程序,该应用程序有一个房间页面。酒店房间可以为他们打开不同种类的门票,在我的房间 View 中,显示了房间号,我在我的助手中使用这个查询来获取单个房间的门票数量

helper/room.rb
SELECT COUNT(*) FROM "tickets" WHERE "tickets"."room_id" = ? AND (status = ? OR status = ? )

room.slim

- @rooms.each do |floor_number, rooms|

- if rooms.present?
h1.separator
- if (@current_view.eql? "room_type") || (params[:action].eql? "room_type")
span= floor_number
- else
span= "Floor-#{floor_number}"

.cards-container
- rooms.each do |room|

= link_to get_show_page_url(room) do
.col-md-3.room-card_room.new-floor-other{ style="background-color:#{card_background_color(room, @maintenance_type, @inspection_type)}" }
.new-floor-inner
- if room.occupy_or_dnd? && (!@current_view.eql? "maintenance") && (!@current_view.eql? "inspection")
span
- if room.dnd?
= image_tag('dnd', class: "guest-occupy-img")
- else
= image_tag('man_to_white', class: "guest-occupy-img")

h4= room.location
- if (@current_view.eql? "maintenance") || (@current_view.eql? "inspection")
h6.room_status
| (
= last_maintained(room, @maintenance_type, @inspection_type)
| )

span.room_hilton_honors= room.hilton_honors
- count = room.open_tickets_count || 0
- if count > 0 && (!@current_view.eql? "maintenance") && (!@current_view.eql? "inspection")
span.circle = count

room_controller
before_action :get_room, only: [:show, :update]
def get_room
@room = Room.find_by_id(params[:id])
@featured_tickets = ImportTicket.where(request_type: "PREVENTIVE MAINTENANCE", featured:true)
end

room.open_tickets_count 是多次触发的查询,open_tickets 在助手内部定义,计数显示该房间的票数

这工作正常,但就性能而言,它会产生更多的处理时间,因为在单个请求中会多次触发此单个查询。假设有 100 个房间,查询被触发 100 次以检查每个房间的门票总数

有没有一种方法可以让我以良好的性能时间实现这一目标?

最佳答案

我假设您有一个包含所有房间 ID 的变量。然后你可以对这个数组使用where,对group使用count:

Ticket.where(room_id: room_ids).group(:room_id).count

返回将如下所示:

{ 1 => 5, 2 => 3 }

这意味着房间 1 有 5 张票,房间 2 有 3 张票。

关于Ruby-on-rails Postgres 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com