Rails 2.3 : Dynamic Scopes
scoped_by_
Order.scoped_by_customer_id(12) Order.scoped_by_customer_id(12).find(:all, :conditions => "status = 'open'") Order.scoped_by_customer_id(12).scoped_by_status("open")
Rails 2.3 : Default Scopes
default_scope(options = {})
class Article < ActiveRecord::Base default_scope :order => 'created_at DESC' end Article.find(:all) #=> "SELECT * FROM `articles` ORDER BY created_at DESC" Article.with_exclusive_scope { find(:all) } #=> "SELECT * FROM `articles`
Ruby on Rails 2.3 Release Notes - 4.4 Default Scopes
Class: ActiveRecord::Base
Rails 2.3 : Batch Processing
find_in_batches / find_each
Invoice.find_in_batches(:include => :invoice_lines) do |invoices| export.add_invoices(invoices) end
User.find_each do |user| NewsLetter.weekly_deliver(user) end
Ruby on Rails 2.3 Release Notes - 4.5 Batch Processing
Active Record Query Interface - 1.3 Retrieving Multiple Objects in Batches
Rails 2.3 : Object#try
you can avoid nil-checking
@person.try(:name) # @person ? @person.name : nil @people.try(:collect) {|p| p.name} Person.try(:find, 1) Product.find_by_price(4.95).try(:name)
Ruby on Rails 2.3 Release Notes - 7.1 Object#try
Class: Object
Railscasts - Rails 2.3 Extras
rescue_from
Rescue exceptions raised in controller actions.
class ApplicationController < ActionController::Base rescue_from ActionController::RoutingError, :with => :route_not_found private def route_not_found(exception) render :text => exception.message.to_s, :status => :not_found end end
has_many :bugs, :through => :rails: Rescue from dispatching
Module: ActiveSupport::Rescuable::ClassMethods
ヽ( ・∀・)ノくまくまー(2008-10-25) rescue_from (ActiveSupport::Rescuable)
Rails 2.2 : Memoization
Memoization is a pattern of initializing a method once and then stashing its value away for repeat use.
extend ActiveSupport::Memoizable def full_name "#{first_name} #{last_name}" end memoize :full_name
Ruby on Rails 2.2 Release Notes - 9.1 Memoization
ヽ( ・∀・)ノくまくまー(2008-10-25) Memoization (ActiveSupport::Memoizable)
send_file
ファイルのダウンロード
send_file '/path/to.jpeg', :type => 'image/jpeg'