Skip to content

Instantly share code, notes, and snippets.

@alexdean
Last active February 9, 2017 23:23
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 alexdean/1edd9def0e83337fea93c97377a121b6 to your computer and use it in GitHub Desktop.
Save alexdean/1edd9def0e83337fea93c97377a121b6 to your computer and use it in GitHub Desktop.
class TempModelBuilder
def self.build(sql, size)
final_table_name = "tmp_#{Digest::MD5.hexdigest(sql)}"
clazz = Class.new(ActiveRecord::Base)
clazz.table_name = final_table_name
# do stuff to create & populate the table
clazz
end
end
# you can create as many of these as you want.
model1 = TempModelBuilder.build('select * from ...', 50)
model2 = TempModelBuilder.build('select * from ...', 50)
# do activerecordish things now
result = model1.joins(:whatever).count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment