class TaskController < ApplicationController def incomplete @tasks = Task.find(:all, :conditions => ['complete = ?', false]) end def last_incomplete @task = Task.find(:first, :conditions => ['complete =?', false], :order => 'created_at DESC') end end
这段代码定义了incomplete和last_incomplete两个函数,作用分别是返回所有未完成的任务和返回最后一个未完成的任务,为了让代码看起来更加优雅,我们可以改写为
@tasks=Task.find_all_by_complete(false)和@task=Task.find_by_complete(false,:order=>'created_at DESC')