Borrowing create_class
from the article you linked, how about something like this:
TABLE_NAMES = %w[customers products purchases]
def create_class(class_name, superclass, &block)
klass = Class.new superclass, &block
Object.const_set class_name, klass
end
def create_model_class(table_name)
create_class(table_name.classify, ActiveRecord::Base) do
scope :in_date_range, lambda {|start_date, end_date| where("start_date >= ?", start_date).where("end_date <= ?", end_date) }
end
end
TABLE_NAMES.each {|table_name| create_model_class(table_name) }
If you wanted to use all tables in the database, you could do this:
TABLE_NAMES = ActiveRecord::Base.connection.tables
But unless you're treating all tables identically, I'm guessing you wouldn't want to do that.
Once you've created your model classes, you should now be able to print objects from each table with their respective columns:
def print_attributes(model_obj)
puts model_obj.attributes.map {|k,v| "#{k}=#{v.inspect}" }.join(', ')
end
Customer.in_date_range(Date.today, Date.today + 1.day).each {|c| print_attributes(c) }
Product.in_date_range(Date.yesterday, Date.today + 2.days).each {|c| print_attributes(c)
Purchase.in_date_range(Date.yesterday, Date.today + 3.days).each {|c| print_attributes(c) }