Skip to content

Instantly share code, notes, and snippets.

@tomafro
Created February 11, 2010 11:02
Show Gist options
  • Save tomafro/301420 to your computer and use it in GitHub Desktop.
Save tomafro/301420 to your computer and use it in GitHub Desktop.
module ColumnReader
def column_reader(column_name, options = {})
name = options.delete(:as) || column_name.to_s.pluralize
column = columns_hash[column_name.to_s]
self.module_eval %{
def self.#{name}
query = scoped.arel.project(arel_table[:#{column_name}])
connection.select_all(query.to_sql).collect do |value|
v = value.values.first
#{column.type_cast_code('v')}
end
end
}
end
ActiveRecord::Base.extend(self)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment