#152 Rails 2.3 Extras
This episode finishes up this series on Rails 2.3. Here you will learn about several smaller additions in 2.3.
- Download:
- source codeProject Files in Zip (94.8 KB)
- mp4Full Size H.264 Video (12.6 MB)
- m4vSmaller H.264 Video (9.24 MB)
- webmFull Size VP8 Video (24.5 MB)
- ogvFull Size Theora Video (15.7 MB)
Resources
- Rails 2.3 RC2 Announcement
- Rails 2.3 Release Notes
- Episode 108: named_scope
- Full Episode Source Code
bash
gem install rails --source http://gems.rubyonrails.org
gem install rails --source http://gems.rubyonrails.org
ruby
# find in batches
Product.count
Product.find_in_batches(:batch_size => 10) do |batch|
puts "Products in batch: #{batch.size}"
end
Product.each(:batch_size => 10) do |product|
puts product.name
end
# scoped_by
Product.scoped_by_price(4.99).size
Product.scoped_by_price(4.99).first
Product.scoped_by_price(4.99).scoped_by_category_id(3).first
# try method
Product.find_by_price(4.99).name
Product.find_by_price(4.95).name
Product.find_by_price(4.95).try(:name)
Product.find_by_price(4.99).try(:name)
# find in batches Product.count Product.find_in_batches(:batch_size => 10) do |batch| puts "Products in batch: #{batch.size}" end Product.each(:batch_size => 10) do |product| puts product.name end # scoped_by Product.scoped_by_price(4.99).size Product.scoped_by_price(4.99).first Product.scoped_by_price(4.99).scoped_by_category_id(3).first # try method Product.find_by_price(4.99).name Product.find_by_price(4.95).name Product.find_by_price(4.95).try(:name) Product.find_by_price(4.99).try(:name)
product.rb
default_scope :order => "name"
default_scope :order => "name"
categories_controller.rb
render 'new'
render 'products/edit'
render 'new' render 'products/edit'
products/index.html.erb
<%= render @products %>
<%= render @products %>
products/show.html.erb
<%= render @product %>
<%= render @product %>
