【发布时间】:2019-01-08 02:38:01
【问题描述】:
我有一个用于酒店的 ruby on rails 应用程序,该应用程序有一个房间页面。酒店房间可以为他们打开不同种类的门票,在我的房间视图中,房间号会显示出来,我在我的助手中使用这个查询来获取单个房间的门票数量
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 次以检查每个房间的门票总数
有没有一种方法可以让我在良好的性能时间内实现这一目标?
【问题讨论】:
-
为什么不在一次查询中加载所有房间的所有票?请详细说明您如何触发查询以及如何使用结果。
-
我正在使用 for 循环来获取要显示的房间数量,并从循环内部调用该查询,使用房间 ID 作为当前在循环中的房间号 @spickermann,我这样做是为了只计算那个特定房间的票数
-
您能否编辑您的问题,向我们展示您如何选择要显示的房间?
-
在控制器中设置
@room,但视图使用“@rooms”。这没有意义。
标签: ruby postgresql ruby-on-rails-5