Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41612
  • 博文数量: 27
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-24 00:44
文章分类
文章存档

2011年(1)

2009年(26)

我的朋友
最近访客

分类: Java

2009-04-17 15:26:12

class Project < ActiveRecord::Base
    has_many :tasks
end

class Task < ActiveRecord::Base
    belongs_to :project
end


class ProjectsController < ApplicationController
    def show
       @project = Project.find(params[:id])
       @tasks = Task.find(:all, :conditions => ['project_id = ? and complete = ?', @project.id, false])
    end
end

This is the stupid way doing this job, let try to find a smarter way to do this, using the association between project and task model.

def show
    @tasks = @project.tasks.find(:all, :conditions => ['complete = ?', false])
end

or, even better way:

def show
    @tasks = @project.tasks.find_all_by_complete(false)
end

That's to say, project_id can be removed from the query conditions, since it is in the mean of the associations.


阅读(314) | 评论(0) | 转发(0) |
0

上一篇:Dynamic find_by Methods

下一篇:Move find into model

给主人留下些什么吧!~~