In this episode we’ll talk about with_scope. Below is a Task model that has a class method that returns all of the incomplete tasks.
class Task < ActiveRecord::Base
belongs_to :project
def self.find_incomplete
find_all_by_complete(false, :order => 'created_at DESC')
end
end
This method is used in the TasksController.
class TasksController < ApplicationController
def index
@tasks = Task.find_incomplete
end
This is a little limiting as we can’t add conditions to the custom find method to find, say, the first twenty incomplete tasks. What we want to do is something like
@tasks = Task.find_incomplete :limit => 20
One way to do this is to add an options hash to the find_incomplete method in the model and merge it with the options in the find, but a more elegant solution exists with find_scope, passing the find options.
class Task < ActiveRecord::Base
belongs_to :project
def self.find_incomplete(options = {})
with_scope :find => options do
find_all_by_complete(false, :order => 'created_at DESC')
end
end
end
Any find executed within the with_scope block will automatically inherit the specified options. Now the find_incomplete can take any conditions passed. This will also work within another scope. Our custom method is called in the ProjectsController. We can pass a limit condition here as well. This find passes through two scopes: first the incomplete tasks are found within the scope of the specified project and secondly they are limited to the first twenty.
@tasks = @project.tasks.find_incomplete :limit => 20
分享到:
相关推荐
Using Server Push with Perl G From Here G Chapter 7 Dynamic and Interactive HTML Content in Perl and CGI Creating User-Specific Pages Allowing the User to Shop for Options H Building an ...
Scope of This Book The first section of this book explains how to install the SAS University Edition and the virtualization software needed to run it. Readers of this book may also be using SAS Studio...
This book aims to provide the student with some basic modelling skills that will have application to a wide variety of problems. The focus is on those mathematical techniques that are applicable to ...
Why Start with the Domain Model Instead of Use Cases?. . . . . . . . 25 Domain Modeling in Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Top 10 Domain Modeling...
23 Working with Data in the Cloud with ADO.NET Data Services 520 Adding ADO.NET Data Services to Your Project 521 Accessing ADO.NET Data Services via HTTP URIs 522 Writing Code with the ADO.NET...
The scope of this book is using J2EE 1.3 on the JBoss 3.2.x series. At the time of writing, the latest release is version 3.2.3. You should use this version or later with the examples. We will cover ...
hands-on guide that provides you with clear and pragmatic information to take advantage of the real power behind HTML5 and Java EE technologies. This book also gives you a good foundation for using ...
After reading this book, you will understand the scope and power of the templates that the C++ 14 Standard Library includes and how these can greatly reduce the coding and development time for many ...
By the end of this book, you will be familiar with the scope of web development using Angular, Swagger, and Docker, learning patterns and practices to be successful as an individual developer on the ...
Several fixes for refactoring and navigation for partially qualified symbols brought into scope with the C++ using namespace directive. (case=5315, case=7148, case=20644) 6573, 5959, 5219 Fix for ...
Automated Trading with R provides automated traders with all the tools they need to trade algorithmically with their existing brokerage, from data management, to strategy optimization, to order ...
Understand the important concepts of OOP in JavaScript, such as scope, objects, inheritance, event delegation, and more Find out how the module design pattern is used in OOP in JavaScript Design and ...
A new approach to pattern recognition using microARTMAP together with wavelet transforms in the context of hand written characters, gestures and signatures have been dealt. The Kohonen Network, Back ...
hands-on guide that provides you with clear and pragmatic information to take advantage of the real power behind HTML5 and Java EE technologies. This book also gives you a good foundation for using ...
5.3 Using Photosynth with Silverlight; 5.4 Key Points; Chapter 6: Media, Webcams, and Video; 6.1 Media in Silverlight; 6.2 Using the MediaElement Control; 6.3 Using a Webcam with Silverlight; 6.4 Key...
How T-SQL provides you with the means to create tools for managing databases of different size, scope, and purpose * Various programming techniques that use views, user-defined functions, and ...
Using GDB with Different Languages r Switching between source languages s List of filename extensions and languages s Setting the working language s Having GDB infer the source language r Displaying ...
Many of the points brought up describe various pitfalls and bugs that lie outside the scope of a typical reference book. The mechanics of ...
2.5 Using Liferay’s Workflow with WCM . . . . . . . . . . . . . . . . . . . 45 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3 Advanced Web Content Management 49 ...
Try It Out: Working with XML Using DOM 290 Try It Out: Working with XML Using SAX 292 Intro to XSLT 293 XSLT Is XML 293 Transformation and Formatting Language 293 Functional,Template-Driven 293 ...