Я пытаюсь передать параметр в сериализатор ActiveModel без какой-либо удачи. Я пытаюсь передать pass params. Я думаю, что это должно быть похоже на params [: booking_items_attributes] [: entity_type]. Я не уверен, и я новичок в этом рельсы.
Мой контроллер
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
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 BookingItemSerializer < ActiveModel::Serializer
attributes :id, :availabitity
belongs_to :booking, serializer: BookingItemSerializer
def availabitity
binding.pry
if params[:booking_items_attributes][:entity_type] == "PrivateOffice"
availabitity = object.private_offices.sum(:count) - booked
elsif params[:booking_items_attributes][:entity_type] == "Desk"
availabitity = object.desks.sum(:count) - booked
elsif params[:booking_items_attributes][:entity_type] == "MeetingRoom"
availabitity = object.meeting_rooms.sum(:count) - booked
end
end
end