bay6/railscasts — pull request #87 by xiongbo (2013-08-23 13:58:51+00:00)
Title: finished 22r

看看就行
## approach
* use includes to avoid N+1 problem

`Product.order("name").includes(:category)`

* use joins to handle associated data

`Product.order("categories.name").includes(:category)`

* use select to fetch part data

`Product.order("categories.name").includes(:category).select('products.*, categories.name as category_name')`
_?q this also cause N+1 problem_

* more on joins

```ruby
Product.joins(:category, :reviews)
# SELECT "products".* FROM "products" INNER JOIN "categories" ON "categories"."id" = "products"."category_id" INNER JOIN "reviews" ON "reviews"."product_id" = "products"."id"

Product.joins(:category, :reviews => :user)
# SELECT "products".* FROM "products" INNER JOIN "categories" ON "categories"."id" = "products"."category_id" INNER JOIN "reviews" ON "reviews"."product_id" = "products"."id" INNER JOIN "users" ON "users"."id" = "reviews"."user_id"

Product.joins("left outer join categories on category_id = categories.id")
# SELECT "products".* FROM "products" left outer join categories on category_id = categories.id
```