Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A look at the differences of query outputs between :includes, :preload, :eager_load methods provided by Rails' ActiveRecord
# ActiveRecord::Base.logger = Logger.new(STDOUT)
Speaker.count # 1873
Talk.count # 1576
Speaker.includes(:talks).to_a
# SELECT `speakers`.* FROM `speakers`
# SELECT `talks`.* FROM `talks` WHERE `talks`.`speaker_id` IN (1, 2, 3,...(omitted)
Speaker.preload(:talks).to_a
# SELECT `speakers`.* FROM `speakers`
# SELECT `talks`.* FROM `talks` WHERE `talks`.`speaker_id` IN (1, 2, 3,...(omitted))
Speaker.eager_load(:talk).to_a
# SELECT `speakers`.`id` AS t0_r0, `speakers`.`member_id` AS t0_r1,...(omitted)
# FROM `speakers`
# LEFT OUTER JOIN `talks` ON `talks`.`speaker_id` = `speakers`.`id`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment