Move a find into the model to clean up the controllers and remove duplication. Also see how you can call these custom find methods through an association.
# tasks_controller.rb
def index
@tasks = Task.find_incomplete
end
# models/task.rb
def self.find_incomplete
find_all_by_complete(false, :order => 'created_at DESC')
end
# projects_controller.rb
def show
@project = Project.find(params[:id])
@tasks = @project.tasks.find_incomplete
end
本文介绍了一种通过在模型中引入自定义查找方法来减少控制器中的重复代码的方法。此外,还展示了如何通过关联调用这些自定义查找方法。
1万+

被折叠的 条评论
为什么被折叠?



