Last active
August 29, 2015 14:15
-
-
Save woodie/2c86aa9d4d2b6d9be43a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 |
This file contains hidden or 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
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