diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-04-17 20:55:24 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-04-17 20:55:24 +0100 |
commit | 28146378d3c83ac8c0ea3427b6152ea61976d642 (patch) | |
tree | 9d0ed7d5a95f1c73804d654f2edb6737a9f45a3c /activerecord | |
parent | 256b363eeecf6d0fb896aabd3fc619e200a5062c (diff) | |
download | rails-28146378d3c83ac8c0ea3427b6152ea61976d642.tar.gz rails-28146378d3c83ac8c0ea3427b6152ea61976d642.tar.bz2 rails-28146378d3c83ac8c0ea3427b6152ea61976d642.zip |
Bring back some bits of documentation for scopes which were removed as part of the reversion in 256b363
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 60840e6958..588f52be44 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -51,6 +51,14 @@ module ActiveRecord # The above calls to <tt>scope</tt> define class methods Shirt.red and Shirt.dry_clean_only. Shirt.red, # in effect, represents the query <tt>Shirt.where(:color => 'red')</tt>. # + # Note that this is simply 'syntactic sugar' for defining an actual class method: + # + # class Shirt < ActiveRecord::Base + # def self.red + # where(:color => 'red') + # end + # end + # # 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.first</tt>, <tt>Shirt.red.count</tt>, <tt>Shirt.red.where(:size => 'small')</tt>. @@ -77,11 +85,31 @@ module ActiveRecord # Named \scopes can also be procedural: # # class Shirt < ActiveRecord::Base - # scope :colored, lambda {|color| where(:color => color) } + # scope :colored, lambda { |color| where(:color => color) } # end # # In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts. # + # On Ruby 1.9 you can use the 'stabby lambda' syntax: + # + # scope :colored, ->(color) { where(:color => color) } + # + # Note that scopes defined with \scope will be evaluated when they are defined, rather than + # when they are used. For example, the following would be incorrect: + # + # class Post < ActiveRecord::Base + # scope :recent, where('published_at >= ?', Time.now - 1.week) + # end + # + # The example above would be 'frozen' to the <tt>Time.now</tt> value when the <tt>Post</tt> + # class was defined, and so the resultant SQL query would always be the same. The correct + # way to do this would be via a lambda, which will re-evaluate the scope each time + # it is called: + # + # class Post < ActiveRecord::Base + # scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) } + # end + # # Named \scopes can also have extensions, just as with <tt>has_many</tt> declarations: # # class Shirt < ActiveRecord::Base |