#<Booking Item Serializer:0x0000558b758121c0>的未定义方法`booking_items'

 收藏

我试图创建一个预订空间API。我收到此错误。有人可以帮我弄这个吗。还有其他方法可以调用booking_items_attribute。我正在建立一个太空预定API

 NoMethodError in Api::V1::BookingsController#create
undefined method `booking_items' for #<BookingItemSerializer:0x0000558b758121c0>
Extracted source (around line #12):
10
11
12
13
14


  def booked
    self.booking_items.sum(:count)
  end
end

预订项目序列化器

class BookingItemSerializer < ActiveModel::Serializer
  attributes  :entity_type, :count,  :booked


  belongs_to :booking,  serializer: BookingItemSerializer
  belongs_to :meeting_rooms, serializer: MeetingRoomSerializer
  belongs_to :private_offices, serializer: PrivateOfficeSerializer
  belongs_to :desks, serializer: DeskSerializer
  belongs_to :operating_hours, serializer: OperatingHourSerializer

  def booked
    self.booking_items.sum(:count)
  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

  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 BookingSerializer < ActiveModel::Serializer
  attributes :id, :user_id, :space_id, :status
  has_many :booking_items, serializer: BookingItemSerializer

end

要求的json

{
   "booking": {
       "space_id": 17,
       "booking_items_attributes": [
           {
               "entity_type": "PrivateOffice",
               "entity_id": 20,
               "count": 1
           },
            {
               "entity_type": "Desk",
               "entity_id": 20,
               "count": 1
           },
            {
               "entity_type": "PrivateOffice",
               "entity_id": 20,
               "count": 1
           }
       ],
        "booking_dates_attributes": [
           {
               "from_date": "2020-03-18",
               "to_date": "2020-03-18",
               "from_time": "2020-03-18 10:00:00 ",
               "to_time": "2020-03-18 14:00:00 "
           }]
   }
}

这就是输出json的样子

{
    "message": "success",
    "data": {
        "booking": {
            "id": null,
            "user_id": 31,
            "space_id": 17,
            "status": null,
            "booking_items": [
                {
                    "entity_type": "PrivateOffice",
                    "count": 1,
                    "booked" :1
                },
                {
                    "entity_type": "Desk",
                    "count": 1
                    "booked" :1
                },
                {
                    "entity_type": "PrivateOffice",
                    "count": 1
                    "booked" :1
                }
            ]
        }
    }
}
回复