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
分享到:
相关推荐
在使用`include`时,还可以结合其他选项,如`required`(是否必须包含关联数据)、`attributes`(选择返回哪些属性)、`through`(对于多对多关联,指定中间表)等,以进一步定制你的查询。这些选项有助于优化查询...
Work with over 40 packages to draw inferences from complex datasets and find hidden patterns in raw unstructured data About This Book Unlock and discover how to tackle clusters of raw data through ...
so that students can in their spare time, to find personal hobbies, through the cultivation of interest, can further carry out the related hobbies. College students'associations are usually managed ...
UltraEdit and UEStudio give you the ability to perform a find or replace through one or more files. Learn how to use UltraEdit/UEStudio's powerful find and replace. Multiline find and replace Search ...
Books Worldwide, through a joint venture with IDG’s Hi-Tech Beijing, became the first U.S. publisher to publish a computer book in the People’s Republic of China. In record time, IDG Books World...
This Learning Path will take you through the fundamentals of R and demonstrate how to use the language to solve a diverse range of challenges through machine learning. Accessible yet comprehensive, it...
**ActiveRecord查询接口**:这是Rails中最基础的查询方式,如`Model.find(id)`用于根据ID获取记录,`Model.where(condition)`用于根据条件筛选记录,`Model.order(column)`用于排序,`Model.includes(:association)`...
3. "Every time you do, you will be certain to find something that you have never seen before." 这句话鼓励我们不断尝试,每次都会发现新事物。 4. "Every time you follow it up, explore, before you know it...
- **Library View:** Once the media library is created, users can view it in an organized manner, making it easier to find specific files. The library view displays the media files in a list format, ...
3. "Some confusion has ____ about who can join the association." 这里需要一个动词表示出现或产生。A"arisen"意为出现,B"lifted"意为举起,C"raised"意为提高,D"retained"意为保留。根据上下文,正确答案是A,...
the activity of moving through water using one's body. - *年龄n*: Age; the length of time that a person has lived or existed. **Unit 4: My day** - *相貌,容貌n*: Appearance; the way someone looks. ...
10.zip Owner Drawn Font Selection ComboBox 自画的字体选择ComboBox(5KB)<END><br>11,add.zip This sample was developed on stage at the Washington Software Association's WinSIG meeting on the ...