begin29
11/24/2016 - 10:39 AM

includes, preload, eager_loading

includes, preload, eager_loading

# :includes delegates to :eager_loading or :preload
# :eager_loading - one big query
# :preload - few small queries


# get only polish adresses then preload all 
User.joins(:addresses).where("addresses.country = ?", "Poland").preload(:addresses)
# SELECT "users".* FROM "users"
# INNER JOIN "addresses" ON "addresses"."user_id" = "users"."id"
# WHERE (addresses.country = 'Poland')

# SELECT "addresses".* FROM "addresses" WHERE "addresses"."user_id" IN (1)

# get only polish adresses
User.joins(:addresses).where("addresses.country = ?", "Poland").includes(:addresses)