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

Using with_scope

阅读更多
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  
分享到:
评论

相关推荐

    JSF 如何傳遞物件 在不同頁面 不同controller in request scope1

    - http://planet.jboss.org/post/learning_jsf2_using_flash_scope - https://snipt.net/raw/36b9c68e087a18d16e884df3fb8030be/?nice 通过以上步骤,你可以在不依赖Session Scope的情况下,在不同页面的不同控制器...

    Using Perl For Web Programming.pdf

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

    Biostatistics by Example Using SAS Studio

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

    Angular 6 for Enterprise-Ready Web Applications pdf

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

    VA_X_Setup1940

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

    Modular Programming with JavaScript(PACKT,2016)

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

    Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON(PACKT,2013)

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

    Problem Solving with C++ (7th edition)

    - **Walkthrough of Functions and Local Variables**: Explanation of how functions interact with local variables, including parameter passing and scope. - **Return Types and Values**: Discussion of ...

    Microsoft.Press.Microsoft.Silverlight.4.Step.by.Step

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

    Beginning T-SQL with Microsoft SQL Server 2005 and 2008

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

    Hands-On With AngularJS Using ASP.NET.pdf

    4. 控制器方法的继承与作用域(Scope) 作用域是连接AngularJS视图与控制器的桥梁。文档中会解释作用域如何继承,以及如何在父子作用域间共享数据。 5. 使用ng-init和ng-repeat指令 ng-init用于初始化应用中的数据...

    COM+ programming [electronic resource] : a practical guide using Visual C++ and ATL

    Many of the points brought up describe various pitfalls and bugs that lie outside the scope of a typical reference book. The mechanics of ...

    using-liferay-portal-6.2.pdf

    2.5 Using Liferay’s Workflow with WCM . . . . . . . . . . . . . . . . . . . 45 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3 Advanced Web Content Management 49 ...

    AngularJS Test-driven Development

    This book is a walk-through to using TDD to build an AngularJS application containing a controller, model, and scope. Building on the initial foundational aspects, you will expand to include testing...

    Microsoft Visual C# 2010 Step by Step Mar 2010

    3 Writing Methods and Applying Scope 47 4 Using Decision Statements 73 5 Using Compound Assignment and Iteration Statements 91 6 Managing Errors and Exceptions 109 Part II Understanding the C# ...

Global site tag (gtag.js) - Google Analytics