aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorEadz <eadz@eadz.co.nz>2011-03-19 00:00:50 -0700
committerRyan Bigg <radarlistener@gmail.com>2011-03-31 06:48:05 +1100
commit45d5d6b2683be263ae9c977324633972f318b814 (patch)
tree02ad1924608a32e0a7b2e47f8ff5980bbf9f92ca /activerecord
parentff09d4bd5b19d478def54648251c78d97027040c (diff)
downloadrails-45d5d6b2683be263ae9c977324633972f318b814.tar.gz
rails-45d5d6b2683be263ae9c977324633972f318b814.tar.bz2
rails-45d5d6b2683be263ae9c977324633972f318b814.zip
Documented undocumented feature: Class methods on your model are automatically available on scopes
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/named_scope.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index d291632260..a445f68790 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -99,6 +99,28 @@ module ActiveRecord
#
# Article.published.new.published # => true
# Article.published.create.published # => true
+ #
+ # Class methods on your model are automatically available
+ # on scopes
+ #
+ # class Article < ActiveRecord::Base
+ # scope :pubished, where(:published => true)
+ # scope :featured, where(:featured => true)
+ #
+ # def self.latest_article
+ # order('published_at desc').first
+ # end
+ #
+ # def self.titles
+ # map{|article| article.title}
+ # end
+ #
+ # end
+ #
+ # Example usage:
+ # Article.published.featured.latest_article
+ # Article.featured.titles
+
def scope(name, scope_options = {})
name = name.to_sym
valid_scope_name?(name)