Jundefined 方法 `booking_items' for #<BookingItemSerializer:0x0000558b758121c0>

Jundefined method `booking_items' for #<BookingItemSerializer:0x0000558b758121c0>

我正在尝试创建预订 Space API。我收到此错误。有人可以帮我弄这个吗。有没有其他方法可以调用booking_items_attribute。我正在建立 space 预订 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
                }
            ]
        }
    }
}

在你的BookingItemSerializer,

将您的 booked 方法替换为

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