Skip to content

Instantly share code, notes, and snippets.

@woodie
Last active August 29, 2015 14:15
Show Gist options
  • Save woodie/2c86aa9d4d2b6d9be43a to your computer and use it in GitHub Desktop.
Save woodie/2c86aa9d4d2b6d9be43a to your computer and use it in GitHub Desktop.
class SomeStatsModel < ActiveRecord::Base
establish_connection "that_other_stats_db_#{Rails.env}".to_sym
TABLE_NAME = 'some_oddly_named_stats_table'
self.table_name = "some_default_schema.#{TABLE_NAME}"
def self.sum_select(*args)
self.select(*args.map {|s| "SUM(#{s}) AS #{s}"}.join(', '))
end
def self.for(account_specific_schema)
klass = Object.const_set("#{self.to_s}_#{key}", Class.new(self))
klass.table_name = "#{account_specific_schema}.#{TABLE_NAME}"
klass
end
end
SomeStatsModel.for('account_xyz123').select(:app).sum_select(:requests).
where(date: Date.today - 2.days).group(:app).order('requests desc').limit(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment