Mysql rails包括为包含的模型加载整个数据的查询
我有一个rails应用程序,其中有以下模型Mysql rails包括为包含的模型加载整个数据的查询,mysql,ruby-on-rails,Mysql,Ruby On Rails,我有一个rails应用程序,其中有以下模型 BookingHotel has_many :hotel_picture_partners, through: :hotel_pictures has_many :booking_hotel_partner_details, dependent: :destroy BookingHotelPartnerDetail belongs_to :booking_hotel HotelPicturePartner belon
BookingHotel
has_many :hotel_picture_partners, through: :hotel_pictures
has_many :booking_hotel_partner_details, dependent: :destroy
BookingHotelPartnerDetail
belongs_to :booking_hotel
HotelPicturePartner
belongs_to :hotel_picture, dependent: :destroy
我有一个问题如下
@booking_hotel_partner_details = BookingHotelPartnerDetail.unscoped.select(:id, :booking_hotel_id, :partner_booking_hotel_id).includes(booking_hotel: :hotel_picture_partners)
这会给内存带来压力,因为它会加载包含模型的所有数据
是否有一种方法可以仅从预订\酒店和酒店\图片\合作伙伴表加载选择性字段
我还想得到一个activerecord数组作为响应。
pull
方法只加载属性,而不加载整个模型。请检查:
试着像这样重写它:
BookingHotelPartnerDetail.unscoped.select('booking_hotels.id as bh_id'、'hotel_picture_partners.id as hpp_id')。加入(booking_hotel::hotel_picture_partners)
我想加载包含的模型,但Pulk会给我一个数组它似乎是正确的Andrey。这不会加载内存中所有关联的记录,对吗?Raman M,它不会立即加载它,对,您可以在Rails日志中检查此AR查询生成的SQL查询。的可能重复项