Permalink
Browse files

Changing examples: use 'each' instead of 'for in'

  • Loading branch information...
1 parent 0d310d5 commit 0cb812a50ebf5eaf0653efca721704dbb60ffc7d @guilleiguaran guilleiguaran committed May 20, 2011
Showing with 4 additions and 4 deletions.
  1. +4 −4 activerecord/lib/active_record/associations.rb
@@ -665,7 +665,7 @@ def association_instance_set(name, association)
#
# Consider the following loop using the class above:
#
- # for post in Post.all
+ # Post.all.each do |post|
# puts "Post: " + post.title
# puts "Written by: " + post.author.name
# puts "Last comment on: " + post.comments.first.created_on
@@ -674,7 +674,7 @@ def association_instance_set(name, association)
# To iterate over these one hundred posts, we'll generate 201 database queries. Let's
# first just optimize it for retrieving the author:
#
- # for post in Post.find(:all, :include => :author)
+ # Post.find(:all, :include => :author).each do |post|
#
# This references the name of the +belongs_to+ association that also used the <tt>:author</tt>
# symbol. After loading the posts, find will collect the +author_id+ from each one and load
@@ -683,15 +683,15 @@ def association_instance_set(name, association)
#
# We can improve upon the situation further by referencing both associations in the finder with:
#
- # for post in Post.find(:all, :include => [ :author, :comments ])
+ # Post.find(:all, :include => [ :author, :comments ]).each do |post|
#
# This will load all comments with a single query. This reduces the total number of queries
# to 3. More generally the number of queries will be 1 plus the number of associations
# named (except if some of the associations are polymorphic +belongs_to+ - see below).
#
# To include a deep hierarchy of associations, use a hash:
#
- # for post in Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])
+ # Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ]).each do |post|
#
# That'll grab not only all the comments but all their authors and gravatar pictures.
# You can mix and match symbols, arrays and hashes in any combination to describe the

0 comments on commit 0cb812a

Please sign in to comment.