Skip to content

Instantly share code, notes, and snippets.

@tomafro
tomafro / scoped_ids.rb
Created August 12, 2008 09:32
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|