# Copyright (C) 2024 Manuel Bustillo class TablesArrangementsController < ApplicationController def index render json: TablesArrangement.all.order(discomfort: :asc).limit(3).as_json(only: %i[id name discomfort]) end def show Seat.joins(guest: :group) .where(tables_arrangement_id: params[:id]) .order('guests.group_id') .pluck( :table_number, Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id', 'groups.color' ) .group_by(&:first) .transform_values { |table| table.map { |(_, name, id, color)| { id:, name:, color: } } } .map { |number, guests| { number:, guests: } } .then { |result| render json: result } end end