Project model has_many tasks:
class Project < ActiveRecord::Base
has_many :tasks
end
a Task model belongs_to a project
class Task < ActiveRecord::Base
belongs_to :project
end
用project控制器的show方法,显示所有未完成tasks:
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@tasks = Task.find(:all, :conditions => ['project_id = ? AND complete = ?', @project.id, false])
end
end
通过model之间的关联去实现是一种更好的方式:
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@tasks = @project.tasks.find(:all, :conditions => [complete = ?', false])
end
end
额,显然,前面介绍过优雅的find_by方法:
class ProjectsController < ApplicationController
def show
@project = Project.find(params[:id])
@tasks = @project.tasks.find_all_by_complete(false)
end
end
分享到:
相关推荐
Find Blue Oceans—Through the Competitive World of Bug Bounty
Find Blue Oceans——Through the Competitive World of Bug Bounty
VB 查找函数Find VB 查找函数Find VB 查找函数Find
gethibernatetemplate的find方法,find(String queryString);find(String queryString , Object value);find(String queryString, Object[] values);findByExample(Object exampleEntity);findByExample(Object ...
解决Cannot find Graphviz.zip
findit插件
matlab 中findpeaks c代码实现,主要实现参数为MinPeakHeight及MinPeakPromince
博客:PostgreSQL的学习心得和知识总结(六十五)|关于PostgreSQL数据库 实现MySQL数据库find_in_set()函数 的实现方案
With BEGINNING C++ THROUGH GAME PROGRAMMING, FOURTH EDITION, you will find an up-to-date and thorough introduction to everything you need to get started--with no previous programming experience ...
解决 cannot find -lxxx 的问题
运用tasklist和find命令来检测进程是否存在
N-FINDR: an algorithm for fast autonomous spectral end-member determination in hyperspectral data 作者Michael E. Winter
新买的Find Reference2插件,经测试可用 https://assetstore.unity.com/packages/tools/find-reference-2-59092 FR2 helps instantly clean up asset references for your projects Email | Documentation | ...
电脑被格式化,分区表丢了,数据误删除,FindData支持WIN98 2000 2003 WINXP 支持NTFS格式,不用花钱做数据恢复了。
Linux系统中的 find 命令在查找文件时非常有用而且方便。它可以根据不同的条件来查找文件,例如权限、拥有者、修改日期/时间、文件大小等等。在这篇文章中,我们将学习如何使用 find 命令以及它所提供的选项来查找...
spring-framework-3.1.1.RELEASE jdk1.7 环境下 can't find referenced pointcut 问题解决 换了对应的jar文件即可 aspectjrt.jar aspectj-1.6.6.jar aspectjweaver.jar
FIND_IN_SET(str,strlist) 定义 假如字符串str在由多个子链组成的字符串列表strlist中,则返回值的范围在1到N之间。 一个字符串列表就是一个由一些被‘,’符号分开的自链组成的字符串。 如果第一个参数是一个...
这款插件叫做Find Code for Research Papers,帮你找论文的源码,甚至第三方实现的源码也可以找到,超级方便可以试试看
windows下的find命令太弱,故写了这个find工具,类似于linux下的find. 首次使用请"findx -h"查看帮助.