提交 0cb812a5 编写于 作者: G Guillermo Iguaran

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

上级 0d310d59
...@@ -665,7 +665,7 @@ def association_instance_set(name, association) ...@@ -665,7 +665,7 @@ def association_instance_set(name, association)
# #
# Consider the following loop using the class above: # Consider the following loop using the class above:
# #
# for post in Post.all # Post.all.each do |post|
# puts "Post: " + post.title # puts "Post: " + post.title
# puts "Written by: " + post.author.name # puts "Written by: " + post.author.name
# puts "Last comment on: " + post.comments.first.created_on # puts "Last comment on: " + post.comments.first.created_on
...@@ -674,7 +674,7 @@ def association_instance_set(name, association) ...@@ -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 # To iterate over these one hundred posts, we'll generate 201 database queries. Let's
# first just optimize it for retrieving the author: # 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> # 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 # symbol. After loading the posts, find will collect the +author_id+ from each one and load
...@@ -683,7 +683,7 @@ def association_instance_set(name, association) ...@@ -683,7 +683,7 @@ def association_instance_set(name, association)
# #
# We can improve upon the situation further by referencing both associations in the finder with: # 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 # 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 # to 3. More generally the number of queries will be 1 plus the number of associations
...@@ -691,7 +691,7 @@ def association_instance_set(name, association) ...@@ -691,7 +691,7 @@ def association_instance_set(name, association)
# #
# To include a deep hierarchy of associations, use a hash: # 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. # 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 # You can mix and match symbols, arrays and hashes in any combination to describe the
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册