Created
August 12, 2008 09:32
-
-
Save tomafro/5024 to your computer and use it in GitHub Desktop.
Get scoped object ids without overhead of instantiating full objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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