Permalink
Browse files

Add pks utility scope

  • Loading branch information...
1 parent e57c04e commit 64b3b97686d1cb498c2a6b8bd87042dfd4217f4c @rwdaigle rwdaigle committed Dec 11, 2009
Showing with 31 additions and 3 deletions.
  1. +9 −1 README.textile
  2. +4 −2 lib/utility_scopes.rb
  3. +18 −0 lib/utility_scopes/pks.rb
View
@@ -137,4 +137,12 @@ using @ordered_by@:
The current default ordering for a class can always be accessed via @default_ordering@:
- Article.default_ordering #=> "published_at DESC"
+ Article.default_ordering #=> "published_at DESC"
+
+h3. PKs
+
+
+@only_pks_@ gets only the primary key column. This is useful when combined with the @pks@ class method to get the primary key values as an array:
+
+ Article.published.limited(10).only_pks # Get the first 10 published articles with only the 'id' value populated [<Article:XX>, <Article:XX>, ...]
+ Article.published.limited(10).pks # Get the first 10 published article ids: [1, 2, 3 ...]
View
@@ -2,14 +2,16 @@
require 'utility_scopes/ordered'
require 'utility_scopes/eager'
require 'utility_scopes/except'
+require 'utility_scopes/pks'
if defined?(ActiveRecord)
ActiveRecord::Base.class_eval do
include UtilityScopes::Limited
include UtilityScopes::Ordered
- include UtilityScopes::Eager
- include UtilityScopes::Except
+ include UtilityScopes::Eager
+ include UtilityScopes::Except
+ include UtilityScopes::Pks
end
end
View
@@ -0,0 +1,18 @@
+module UtilityScopes
+ module Pks
+
+ def self.included(within)
+
+ within.class_eval do
+ named_scope :only_pks, :select => "`#{table_name}`.#{primary_key}"
+ extend ClassMethods
+ end
+ end
+
+ module ClassMethods
+ def pks
+ only_pks.collect(&:"#{primary_key}")
+ end
+ end
+ end
+end

0 comments on commit 64b3b97

Please sign in to comment.