诗歌rails之 Rails 2.3 Extras
1 # find in batches
2 Product.count
3 Product.find_in_batches(:batch_size => 10) do |batch|
4 puts "Products in batch: #{batch.size}"
5 end
6 Product.each(:batch_size => 10) do |product|
7 puts product.name
8 end
9
10 # scoped_by
11 Product.scoped_by_price(4.99).size
12 Product.scoped_by_price(4.99).first
13 Product.scoped_by_price(4.99).scoped_by_category_id(3).first
14
15 # try method
16 Product.find_by_price(4.99).name
17 Product.find_by_price(4.95).name
18 Product.find_by_price(4.95).try(:name)
19 Product.find_by_price(4.99).try(:name)
20
21 # product.rb
22 default_scope :order => "name"
23
24 # categories_controller.rb
25 render 'new'
26 render 'products/edit'
27
28 <!-- products/index.html.erb -->
29 <%= render @products %>
30
31 <!-- products/show.html.erb -->
32 <%= render @product %>
33
34
2 Product.count
3 Product.find_in_batches(:batch_size => 10) do |batch|
4 puts "Products in batch: #{batch.size}"
5 end
6 Product.each(:batch_size => 10) do |product|
7 puts product.name
8 end
9
10 # scoped_by
11 Product.scoped_by_price(4.99).size
12 Product.scoped_by_price(4.99).first
13 Product.scoped_by_price(4.99).scoped_by_category_id(3).first
14
15 # try method
16 Product.find_by_price(4.99).name
17 Product.find_by_price(4.95).name
18 Product.find_by_price(4.95).try(:name)
19 Product.find_by_price(4.99).try(:name)
20
21 # product.rb
22 default_scope :order => "name"
23
24 # categories_controller.rb
25 render 'new'
26 render 'products/edit'
27
28 <!-- products/index.html.erb -->
29 <%= render @products %>
30
31 <!-- products/show.html.erb -->
32 <%= render @product %>
33
34
莫愁前路无知己,天下无人不识君。