Я пытался показать доступность места в API show space. Лог c за кодом - доступность = общее количество доступного пространства - общее количество забронированного пространства. Может кто-нибудь, пожалуйста, помогите мне кодировать это, потому что я новичок в rails API
My Space Controller
# frozen_string_literal: true
# Spaces controller
class Api::V1::SpacesController < ApiController
before_action :fetch_space, only: [:show]
def index
spaces = Space.all
render_collection(spaces, { name: 'spaces' }, each_serializer: SpaceShortSerializer)
end
def show
render json: @space, serializer: SpaceSerializer
end
def create
if space = current_user.spaces.create(space_params)
render_object(space, { name: 'space' }, { serializer: SpaceSerializer })
else
render_error(space.errors.full_messages)
end
end
private
def fetch_space
space_id = params[:space_id] || params[:id]
@space = Space.find(space_id)
end
def space_params
params.require(:space).permit(:name, :email, :website, :phone,
amenities_attributes: [:id, :name, :available],
meeting_rooms_attributes: [:id, :count, :capacity, pricings_attributes: [:id, :duration, :price]],
private_offices_attributes: [:id, :count, :capacity, pricings_attributes: [:id, :duration, :price]],
desks_attributes: [:id, :count, :desk_type, pricings_attributes: [:id, :duration, :price]],
operating_hours_attributes: [:id, :day, :slot])
end
end
Резервирование контроллера
class Api::V1::BookingsController < ApiController
before_action :fetch_space, only: [:show]
def index
bookings = Booking.all
render_collection(bookings, { name: 'bookings' }, each_serializer: BookingItemSerializer )
end
# booking_item_params: params[:entity_type]
def create
if booking = current_user.bookings.create(booking_params)
render_object(booking, { name: 'booking' }, { serializer: BookingSerializer })
else
render_error(booking.errors.full_messages)
end
end
private
def booking_params
params.require(:booking).permit(:space_id,
booking_items_attributes: [:id, :entity_type, :entity_id, :count],
booking_dates_attributes: [:id, :from_date, :to_date, :from_time, :to_time])
end
end
пробел сериализатор
class SpaceSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :website, :phone, :owner, :available_amenities, :photos
belongs_to :owner, except: :profile, serializer: OwnerSerializer
has_many :meeting_rooms, serializer: MeetingRoomSerializer
has_many :private_offices, serializer: PrivateOfficeSerializer
has_many :desks, serializer: DeskSerializer
has_many :operating_hours, serializer: OperatingHourSerializer
def photos
Photo::DUMMY_SPACE_IMAGES.shuffle.first(3)
end
end
Сериализатор бронирования
class BookingSerializer < ActiveModel::Serializer
attributes :id, :user_id, :space_id, :status, :booked
has_many :booking_items, serializer: BookingItemSerializer
def booked
object.booking_items.sum(:count)
end
end
Сериализатор позиции бронирования
class BookingItemSerializer < ActiveModel::Serializer
has_many :meeting_rooms, serializer: MeetingRoomSerializer
has_many :private_offices, serializer: PrivateOfficeSerializer
has_many :desks, serializer: DeskSerializer
has_many :operating_hours, serializer: OperatingHourSerializer
end
Сериализатор конференц-зала
class MeetingRoomSerializer < ActiveModel::Serializer
attributes :id, :count, :capacity, :availabitity, :photos
belongs_to :booking_items, serializer: BookingItemSerializer
def photos
Photo::DUMMY_MEETING_ROOM_IMAGES.shuffle.first(2)
end
def availabitity
object.count - object.booking_items.sum(:count)
end
end
Как и в сериализаторе конференц-зала, мне нужно показать наличие в сериализатор для личного кабинета и настольного компьютера.
сериализатор для личного кабинета
class PrivateOfficeSerializer < ActiveModel::Serializer
attributes :id, :count, :capacity, :photos
def photos
Photo::DUMMY_PRIVATE_OFFICE_IMAGES.shuffle.first(2)
end
end
настольный сериализатор
class DeskSerializer < ActiveModel::Serializer
attributes :id, :count, :desk_type, :photos
def photos
Photo::DUMMY_DESK_IMAGES.shuffle.first(2)
end
end
стек ошибок
Completed 500 Internal Server Error in 150ms (ActiveRecord: 28.3ms | Allocations: 46623)
NoMethodError (undefined method `booking_items' for #<MeetingRoom:0x0000557526c2b9b0>):
app/serializers/meeting_room_serializer.rb:10:in `availabitity'
app/controllers/api/v1/spaces_controller.rb:13:in `show'