`
Hooopo
  • 浏览: 335241 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Find Through Association

阅读更多
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 



分享到:
评论

相关推荐

    关于Sequelize连接查询时inlude中model和association的区别详解

    在使用`include`时,还可以结合其他选项,如`required`(是否必须包含关联数据)、`attributes`(选择返回哪些属性)、`through`(对于多对多关联,指定中间表)等,以进一步定制你的查询。这些选项有助于优化查询...

    Unsupervised.Learning.with.R

    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 ...

    基于jsp学生社团管理系统源码.zip

    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 ...

    UE(官方下载)

    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 ...

    MySQL/PHP Database Applications 英文版

    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...

    R.Unleash.Machine.Learning.Techniques

    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...

    rails查询学习笔记

    **ActiveRecord查询接口**:这是Rails中最基础的查询方式,如`Model.find(id)`用于根据ID获取记录,`Model.where(condition)`用于根据条件筛选记录,`Model.order(column)`用于排序,`Model.includes(:association)`...

    2019_2020学年高中英语Unit3Inventorsandinventions单元要点回顾课件新人教版选修8

    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...

    iMON & iMEDIAN HD User's Guide(ENG)

    - **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

    3. "Some confusion has ____ about who can join the association." 这里需要一个动词表示出现或产生。A"arisen"意为出现,B"lifted"意为举起,C"raised"意为提高,D"retained"意为保留。根据上下文,正确答案是A,...

    (牛津译林版)初中单词汇总(默写版).docx

    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. ...

    Visual C++ 编程资源大全(英文源码 控件)

    10.zip Owner Drawn Font Selection ComboBox 自画的字体选择ComboBox(5KB)&lt;END&gt;&lt;br&gt;11,add.zip This sample was developed on stage at the Washington Software Association's WinSIG meeting on the ...

Global site tag (gtag.js) - Google Analytics