Skip to content

Instantly share code, notes, and snippets.

@tomlea
Created January 6, 2012 12:06
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 tomlea/1570291 to your computer and use it in GitHub Desktop.
Save tomlea/1570291 to your computer and use it in GitHub Desktop.
Select Some Columns
# Introduction:
We need to select large numbers of items (300k or more), based on active record associations (and selectors, and all that faff), but we only care about the ids.
This is easily solved by bla_bla_bla_scope.map(&:id). This however takes about 16s of CPU grinding ruby to get to the final answer via AR objects. AR objects that contain only an ID.
Why not just use the SQL generated by the scopes and other voodoo to just pump the SQL through? That's what this does, in the most complex way imaginable!
# Usage:
Person.ids # => all the person ids. (or whatever the key field is).
Person.order("name").select_name_and_id #=> Sorted names,id pairs.
Person.first.friends.order("name").select_name_and_id #=> Sorted names,id pairs of the first person's friends.
#Initializer:
module ActiveRecord
class DynamicSelectMatch
def self.match(method)
return unless method.to_s =~ /^select_([_a-zA-Z]\w*)$/
new($1 && $1.split('_and_'))
end
def initialize(attribute_names)
@multi = attribute_names.size > 1
@attribute_names = attribute_names
end
attr_reader :multi, :attribute_names
alias :multi? :multi
end
module SelectJustSomeColumns
def select_column_values(*columns)
columns = columns.map{|c| "#{quoted_table_name}.#{connection.quote_column_name(c)}" }
connection.select_rows(scoped(:select => columns.join(", ")).arel.to_sql )
end
def select_column_value(column)
column = "#{quoted_table_name}.#{connection.quote_column_name(column)}"
connection.select_values(scoped(:select => column).arel.to_sql )
end
def ids
select_column_value(primary_key)
end
def method_missing(method_id, *arguments, &block)
if match = DynamicSelectMatch.match(method_id)
super unless all_attributes_exists?(match.attribute_names)
if match.multi?
self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def self.#{method_id} # def self.select_user_name_and_password
select_column_values(:#{match.attribute_names.join(',:')}) # select_column_values(:user_name, :password)
end # end
METHOD
else
col_name = match.attribute_names.first.to_sym.inspect
self.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def self.#{method_id} # def self.select_user_name
select_column_value(#{col_name}) # select_column_values(:user_name)
end # end
METHOD
end
send(method_id, *arguments)
else
super
end
end
def respond_to?(method_id, include_private = false)
if match = DynamicSelectMatch.match(method_id)
return true if all_attributes_exists?(match.attribute_names)
end
super
end
end
Base.extend(SelectJustSomeColumns)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment