<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Module: ActiveRecord::NamedScope::ClassMethods</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" /> <script type="text/javascript"> // <![CDATA[ function popupCode( url ) { window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400") } function toggleCode( id ) { if ( document.getElementById ) elem = document.getElementById( id ); else if ( document.all ) elem = eval( "document.all." + id ); else return false; elemStyle = elem.style; if ( elemStyle.display != "block" ) { elemStyle.display = "block" } else { elemStyle.display = "none" } return true; } // Make codeblocks hidden by default document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" ) // ]]> </script> </head> <body> <div id="classHeader"> <table class="header-table"> <tr class="top-aligned-row"> <td><strong>Module</strong></td> <td class="class-name-in-header">ActiveRecord::NamedScope::ClassMethods</td> </tr> <tr class="top-aligned-row"> <td><strong>In:</strong></td> <td> <a href="../../../files/lib/active_record/named_scope_rb.html"> lib/active_record/named_scope.rb </a> <br /> </td> </tr> </table> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> </div> <div id="method-list"> <h3 class="section-bar">Methods</h3> <div class="name-list"> <a href="#M000366">named_scope</a> <a href="#M000365">scopes</a> </div> </div> </div> <!-- if includes --> <div id="section"> <!-- if method_list --> <div id="methods"> <h3 class="section-bar">Public Instance methods</h3> <div id="method-M000366" class="method-detail"> <a name="M000366"></a> <div class="method-heading"> <a href="ClassMethods.src/M000366.html" target="Code" class="method-signature" onclick="popupCode('ClassMethods.src/M000366.html');return false;"> <span class="method-name">named_scope</span><span class="method-args">(name, options = {}, &block)</span> </a> </div> <div class="method-description"> <p> Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query, such as <tt>:conditions => {:color => :red}, :select => ‘shirts.*’, :include => :washing_instructions</tt>. </p> <pre> class Shirt < ActiveRecord::Base named_scope :red, :conditions => {:color => 'red'} named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true] end </pre> <p> The above calls to <tt><a href="ClassMethods.html#M000366">named_scope</a></tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => ‘red’})</tt>. </p> <p> Unlike <tt>Shirt.find(...)</tt>, however, the object returned by Shirt.red is not an Array; it resembles the association object constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>, <tt>Shirt.red.find(:all, :conditions => {:size => ‘small’})</tt>. Also, just as with the association objects, named \<a href="ClassMethods.html#M000365">scopes</a> act like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>, <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really was an Array. </p> <p> These named \<a href="ClassMethods.html#M000365">scopes</a> are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are both red and dry clean only. Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>. </p> <p> All \<a href="ClassMethods.html#M000365">scopes</a> are available as class methods on the <a href="../Base.html">ActiveRecord::Base</a> descendant upon which the \<a href="ClassMethods.html#M000365">scopes</a> were defined. But they are also available to <tt>has_many</tt> associations. If, </p> <pre> class Person < ActiveRecord::Base has_many :shirts end </pre> <p> then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton‘s red, dry clean only shirts. </p> <p> Named \<a href="ClassMethods.html#M000365">scopes</a> can also be procedural: </p> <pre> class Shirt < ActiveRecord::Base named_scope :colored, lambda { |color| { :conditions => { :color => color } } } end </pre> <p> In this example, <tt>Shirt.colored(‘puce’)</tt> finds all puce shirts. </p> <p> Named \<a href="ClassMethods.html#M000365">scopes</a> can also have extensions, just as with <tt>has_many</tt> declarations: </p> <pre> class Shirt < ActiveRecord::Base named_scope :red, :conditions => {:color => 'red'} do def dom_id 'red_shirts' end end end </pre> <p> For testing complex named \<a href="ClassMethods.html#M000365">scopes</a>, you can examine the scoping options using the <tt>proxy_options</tt> method on the proxy itself. </p> <pre> class Shirt < ActiveRecord::Base named_scope :colored, lambda { |color| { :conditions => { :color => color } } } end expected_options = { :conditions => { :colored => 'red' } } assert_equal expected_options, Shirt.colored('red').proxy_options </pre> </div> </div> <div id="method-M000365" class="method-detail"> <a name="M000365"></a> <div class="method-heading"> <a href="ClassMethods.src/M000365.html" target="Code" class="method-signature" onclick="popupCode('ClassMethods.src/M000365.html');return false;"> <span class="method-name">scopes</span><span class="method-args">()</span> </a> </div> <div class="method-description"> </div> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html>