Skip to content
flyerhzm edited this page Aug 13, 2010 · 3 revisions

Please go to http://rails-bestpractices.com/posts/14-love-named_scope

Before:


class PostsController < ApplicationController

  def search
    conditions = { :title => "%#{params[:title]}%" } if params[:title]
    conditions.merge! { :content => "%#{params[:content]}%" } if params[:content]

    case params[:order]
    when "title" : order = "title desc"
    when "created_at : order = "created_at desc"
    end

    if params[:is_published]
      conditions.merge! { :is_published => true }
    end

    @posts = Post.find(:all, :conditions => conditions, :order => order,
                             :limit => params[:limit])
  end

end

After:


class Post < ActiveRecord::Base

  named_scope :matching, lambda { |column , value|
    return {} if value.blank?
    { :conditions => ["#{column} like ?", "%#{value}%"] }
  }

  named_scope :order, lambda { |order|
    { 
      :order => case order
      when "title" : "title desc"
      when "created_at" : "created_at desc"
      end
    }
  }
end

class PostsController < ApplicationController

  def search
    @posts = Post.matching(:title, params[:title])
                 .matching(:content, params[:content])
                 .order(params[:order])
  end

end