Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created August 12, 2008 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomafro/5024 to your computer and use it in GitHub Desktop.
Save tomafro/5024 to your computer and use it in GitHub Desktop.
Get scoped object ids without overhead of instantiating full objects
class Animal < ActiveRecord::Base
named_scope :with_wings, :conditions => {:wings => true}
named_scope :with_four_legs, :conditions => {:legs => 4}
# Returns object ids falling within current scope without
# instantiating actual model objects
def self.ids
column = columns_hash[primary_key]
ids = connection.select_all(construct_finder_sql(:select => primary_key)).collect do |value|
v = value[primary_key]
# TODO Find nicer way to type cast
eval column.type_cast_code('v')
end
end
end
Animal.ids
Animal.with_wings.ids
Animal.with_wings.with_four_legs.ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment