モデルでfind、ページング

findの処理をモデル側に任せて、かつページングする方法

  def show_articles
    return unless @user = check_valid_user
    @articles = @user.get_articles

  #added for pagenate
    count = @articles.size

    @pages = Paginator.new(self, count, LIMIT_PER_PAGE, params[:page])
    @articles = 
@articles[@pages.current.offset..(@pages.current.offset + 
LIMIT_PER_PAGE - 1)]
  end

 def add_article(writer,article)
    flag = writer.set_flag
    article.set_flag(flag)
    self.articles << article
  end

  def get_articles
    active_books = self.articles.find(:all,:conditions => ["status = 
?",1],:order => "created_at DESC")
    active_books
    #active_books = self.articles.reject{|book| !book.active?}
    #return active_books.reverse
  end

  def get_latest_article
    articles = get_articles
    articles.first
  end

def paginate_template
    text = ""

    text << '<div align="center" style="text-align:center;">'

    if (@pages.current.previous || @pages.current.next)
      text << '<p>'
      text << colored_hr(@color)
      text << link_to("前へ", :user_id => @user, :page => 
@pages.current.previous) if @pages.current.previous
      text << " | " if (@pages.current.previous && @pages.current.next)
      text << link_to("次へ", :user_id => @user,:page => 
@pages.current.next) if @pages.current.next
      text << colored_hr(@color)
      text << '</p>'
    end

    text << '</div>'
    text
  end

この方法なら、SQLの結果に対し、Rubyで処理した後にページネートを使える。