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

Move Find Into Model

阅读更多
下面是在控制器里面通过model查找未完成的任务:

class TaskController < ApplicationController  
  def index  
    @tasks = Task.find_all_by_complete_(false, :order => "created_at DESC")  
  end  
end  

If the find is being performed several times throughout the application then there will be duplication. One way to remove this duplication is to move the find into the model, which would allow us to call
@tasks = Task.find_incomplete  

in the controller. To do this we’ll need to create a new method in the model. The method has to be a class method so must start with self.
class Task < ActiveRecord::Base  
  belongs_to :project  
  
  def self.find_incomplete  
    find_all_by_complete_(false, :order => "created_at DESC")  
  end  
end  

There’s no need to specify Task in the find line as the find is already scoped inside the Task class. Now you can call Task.find_incomplete whenever you need to perform that find. This will even work in associations so that you can find Project’s incomplete tasks like this:
class ProjectsController < ApplicationController  
    def show  
        @project = Project.find(params[:id])  
        @tasks = @project.tasks.find_incomplete  
    end  
end  

分享到:
评论
1 楼 qichunren 2009-08-05  
Hooopo转帖子为什么不写地址?我要看原帖子,哇哇啦~~·

相关推荐

    a project model for the FreeBSD Project.7z

    However, Niels Jørgenssen has suggested a model of how written code is integrated into the project. Figure 4-1. Jørgenssen's model for change integration The “development release” is the ...

    iOS.Application.Security

    You'l learn about: The iOS security model and the limits of its built-in protections The myriad ways sensitive data can leak into places it shouldn't, such as through the pasteboard How to implement ...

    Xcode 4 Unleashed 2nd Edition (epub 格式)

    Through practical examples, he shows experienced Apple developers how to move to Xcode 4’s “browser” model from older document-based approaches. You’ll also find thorough, up-to-the-minute ...

    IOS 10 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basic

    Move into iOS development by getting a firm grasp of its fundamentals, including the Xcode IDE, the Cocoa Touch framework, and Swift 3 the latest version of Apple s acclaimed programming language....

    iOS 9 Programming Fundamentals with Swift 无水印pdf 0分

    Move into iOS development by getting a firm grasp of its fundamentals, including the Xcode IDE, the Cocoa Touch framework, and Swift 2.0—the latest version of Apple's acclaimed programming language....

    Test-Driven iOS Development with Swift

    Test-driven development (TDD) is a proven way to find software bugs early. Writing tests before your code improves the structure and maintainability of your app. Key Features Learn test-driven ...

    React and React Native [Kindle Edition]

    We then move on to showing you how to take the concepts of React and apply them to building Native UIs using React Native. You'll find out how to build responsive and streamlined UIs that can properly...

    kgb档案压缩console版+源码

    The data is compressed as if all the files were concatenated into one long string. PAQ6 uses predictive arithmetic coding. The string, y, is compressed by representing it as a base 256 number, x, ...

    网络协议攻击

    application protocol into flexible layers and abstracts complex technical detail, allowing you to focus on the bespoke parts of the protocol you’re analyzing. Chapter 2: Capturing Application Traffic...

    Java.for.Bioinformatics.and.Biomedical.Applications.(2007).part1.rar

    it takes a deep dive into demonstrating how to build highly functional graphical user interfaces for common and widely used bioinformatics tools that most researchers are familiar with and find ...

    The.Agile.Consultant.Guiding.Clients.to.Enterprise.Agility.epub

    Many organizations have brought in agile coaches and achieved great progress in software development productivity, only to find teams slipping back into old methods as they encounter enterprise ...

    AI_01_sfuadsadad

    2. **Search**: Search algorithms are fundamental to AI, enabling machines to find solutions to problems by exploring possible states or actions. Techniques such as breadth-first search, depth-first ...

    [3planesoft屏幕保护程序合集].Premium.3D.Screensavers.iso

    Find yourself in a mysterious dwelling gnomes! Admire the impressive underground hall with antique fireplace, where the light is refracted in the columns and arches made of rock crystal, and at the ...

    swing文件拖拽

    // Ask the cell renderer to paint itself into the BufferedImage g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f)); // Make the image ghostlike lbl.paint(g2); g2.dispose(); /...

    Java邮件开发Fundamentals of the JavaMail API

    further than the JavaMail API, which offers a protocol-independent model for working with IMAP, POP, SMTP, MIME, and all those other Internet-related messaging protocols. With the help of the ...

    gertt点阵图读取器(opengl)

    // Move Left 1.5 Units And Into The Screen 6.0 bc=bb=ba=0; glColor3f(0.0,1.0,0.0); while(bc) { if(ax[bc]==1) { ba=bc%100; bb=(bc-ba)/100; glBegin(GL_QUADS); // Draw A ...

Global site tag (gtag.js) - Google Analytics