沿着
Rails宝典之第三式: 通过关联做查询的脚步,我们可以进一步简化代码:
- class Task < ActiveRecord::Base
- belongs_to :project
- def self.find_incomplete
- find_all_by_complete(false, : order => 'created_at DESC')
- end
- end
- class ProjectsController < ApplicationController
- def show
- @project = Project.find(param[:id])
- @tasks = @project.tasks.find_incomplete
- end
- end
- class TasksController < ApplicationController
- def index
- @tasks = Task.find_incomplete
- end
- end
本文介绍了一种使用Rails框架简化关联查询的方法,通过定义特定的类方法实现任务的不完整状态查询,进而提高代码效率。


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



