-
Notifications
You must be signed in to change notification settings - Fork 277
Move finder to named_scope
Richard Huang edited this page Aug 15, 2010
·
5 revisions
Please go to http://rails-bestpractices.com/posts/1-move-finder-to-named_scope
Before:
class PostsController < ApplicationController
def index
@published_posts = Post.find(:all, :conditions => { :state => 'published' },
:limit => 10,
:order => 'created_at desc')
@draft_posts = Post.find(:all, :conditions => { :state => 'draft' },
:limit => 10,
:order => 'created_at desc')
end
end
After:
class PostsController < ApplicationController
def index
@published_posts = Post.published
@draft_posts = Post.draft
end
end
class Post < ActiveRecord::Base
named_scope :published, :conditions => { :state => 'published' },
:limit => 10, :order => 'created_at desc'
named_scope :draft, :conditions => { :state => 'draft' },
:limit => 10, :order => 'created_at desc'
end