下面是在控制器里面通过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
分享到:
相关推荐
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 ...
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 ...
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 ...
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....
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 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 ...
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...
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...
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 ...
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 ...
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 ...
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 ...
// 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(); /...
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 ...
// 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 ...