includes
includes采用使用left outer join
A.includes(:B) 就是将B model的attribute一次性加载到A中,使得A也可以访问B的attribute,如
Plan.includes(:schedules).where(schedules: { day: 1 })
"SELECT `plans`.`id` AS t0_r0, `plans`.`manager_id` AS t0_r1, `plans`.`planner_id` AS t0_r2, `plans`.`creator_id` AS t0_r3, `plans`.`cost` AS t0_r4, `plans`.`fee` AS t0_r5, `plans`.`old_destination` AS t0_r6, `plans`.`budget` AS t0_r7, `plans`.`travel_time` AS t0_r8, `plans`.`departure_date` AS t0_r9, `plans`.`created_at` AS t0_r10, `plans`.`updated_at` AS t0_r11, `plans`.`return_date` AS t0_r12, `plans`.`travel_days` AS t0_r13, `plans`.`grades` AS t0_r14, `plans`.`cover_pic` AS t0_r15, `plans`.`route_pic` AS t0_r16, `plans`.`dedicate` AS t0_r17, `plans`.`title` AS t0_r18, `plans`.`subtitle` AS t0_r19, `plans`.`requirements` AS t0_r20, `plans`.`uuid` AS t0_r21, `plans`.`orderer_id` AS t0_r22, `plans`.`cancel_reason` AS t0_r23, `plans`.`country_ids` AS t0_r24, `plans`.`destination_id` AS t0_r25, `plans`.`service` AS t0_r26, `plans`.`state` AS t0_r27, `plans`.`deposit_contract_amount` AS t0_r28, `plans`.`formal_contract_amount` AS t0_r29, `plans`.`signed_deposit_contract_at` AS t0_r30, `plans`.`signed_formal_contract_at` AS t0_r31, `plans`.`deleted_at` AS t0_r32, `plans`.`estimated_departure_date` AS t0_r33, `plans`.`idea` AS t0_r34, `plans`.`user_id` AS t0_r35, `plans`.`label` AS t0_r36, `plans`.`explicit_signed_deposit_contract` AS t0_r37, `plans`.`total_average_user_rate` AS t0_r38, `plans`.`guidebook_planner_id` AS t0_r39, `plans`.`is_send_notify` AS t0_r40, `plans`.`next_contact_at_serialized` AS t0_r41, `plans`.`imported_at` AS t0_r42, `plans`.`contact_priority` AS t0_r43, `plans`.`planner_comment` AS t0_r44, `plans`.`manager_comment` AS t0_r45, `plans`.`is_performance_cancel` AS t0_r46, `plans`.`advisor_id` AS t0_r47, `plans`.`category` AS t0_r48, `plans`.`from_plan_id` AS t0_r49, `plans`.`advisor_comment` AS t0_r50, `plans`.`deposit_contract_lock_days` AS t0_r51, `plans`.`from_plan_type` AS t0_r52, `plans`.`type` AS t0_r53, `plans`.`prepaid_idea` AS t0_r54, `plans`.`is_free_customize` AS t0_r55, `plans`.`initial_discount` AS t0_r56, `plans`.`initial_discount_reason` AS t0_r57, `plans`.`guidebook_title` AS t0_r58, `plans`.`en_dedicate` AS t0_r59, `plans`.`next_destination_id` AS t0_r60, `plans`.`place_ids` AS t0_r61, `plans`.`booking_count` AS t0_r62, `plans`.`ticket_id` AS t0_r63, `plans`.`actual_tailor_days` AS t0_r64, `plans`.`is_dist` AS t0_r65, `plans`.`is_audited` AS t0_r66, `plans`.`final_balance` AS t0_r67, `plans`.`distributor_id` AS t0_r68, `plans`.`is_followed` AS t0_r69, `plans`.`verifier_id` AS t0_r70, `plans`.`verified_at` AS t0_r71, `plans`.`deprecater_id` AS t0_r72, `plans`.`deprecated_at` AS t0_r73, `plans`.`departure_place_id` AS t0_r74, `plans`.`cloned_at` AS t0_r75, `plans`.`is_audit_printed` AS t0_r76, `plans`.`sub_type` AS t0_r77, `plans`.`plus_package_price` AS t0_r78, `plans`.`deposit_payment_countdown` AS t0_r79, `plans`.`formal_payment_countdown` AS t0_r80, `plans`.`ordering_countdown` AS t0_r81, `plans`.`suspend_reason` AS t0_r82, `plans`.`deposit_received_at` AS t0_r83, `plans`.`full_received_at` AS t0_r84, `plans`.`deleter_id` AS t0_r85, `plans`.`full_profit_rate` AS t0_r86, `schedules`.`id` AS t1_r0, `schedules`.`plan_id` AS t1_r1, `schedules`.`day` AS t1_r2, `schedules`.`date` AS t1_r3, `schedules`.`node` AS t1_r4, `schedules`.`created_at` AS t1_r5, `schedules`.`updated_at` AS t1_r6, `schedules`.`remark` AS t1_r7, `schedules`.`images` AS t1_r8, `schedules`.`pic` AS t1_r9, `schedules`.`completeness` AS t1_r10, `schedules`.`daily_images` AS t1_r11, `schedules`.`deleted_at` AS t1_r12, `schedules`.`reference_count` AS t1_r13, `schedules`.`error_message` AS t1_r14, `schedules`.`deleter_id` AS t1_r15 FROM `plans` LEFT OUTER JOIN `schedules` ON `schedules`.`plan_id` = `plans`.`id` AND `schedules`.`deleted_at` IS NULL WHERE `plans`.`deleted_at` IS NULL AND `schedules`.`day` = 1"
将整个plan表和schedule表所有字段全部加载,这样如果后面需要多次调用,则不用再进行查询。
joins
但如果我不想将整个table载入,而只是想载入相关字段,即可使用joins,即inner join
joins采用lazy load,通过table之间关系只load 两个表非null的字段,但是后面每次使用我都要重新query。
Plan.joins(:schedules).where(schedules: { day: 1 })
"SELECT `plans`.* FROM `plans` INNER JOIN `schedules` ON `schedules`.`plan_id` = `plans`.`id` AND `schedules`.`deleted_at` IS NULL WHERE `plans`.`deleted_at` IS NULL AND `schedules`.`day` = 1"
总结一下
如果需要多次夸表调用,includes是一种先进的语法,否则joins更加高效一些