2008年5月11日星期日

RailsCasts 003 find through association

关联关系:
Project has_many Tasks
Task belongs_to Project
传统做法:
@project = Project.find(params[:id])
@tasks = Task.find(:all, :conditions => ['project_id = ? AND complete = ?'], @project.id, false)
Rails做法:
@tasks = @project.tasks.find(:all, :conditions => ['complete = ?'], false)
episode 002介绍的动态方法进一步简化:
@tasks = @project.tasks.find_all_by_complete(false)

没有评论: