Skip to content

Instantly share code, notes, and snippets.

@akapitula
Last active November 20, 2025 13:17
Show Gist options
  • Select an option

  • Save akapitula/e7581334a3c43d3b12f0d56ff294d29c to your computer and use it in GitHub Desktop.

Select an option

Save akapitula/e7581334a3c43d3b12f0d56ff294d29c to your computer and use it in GitHub Desktop.
class User < ApplicationRecord
has_many :orders
def generate_monthly_report
report = "Monthly Report for #{name}\n"
report += "=" * 50 + "\n"
total_orders = orders.where('created_at >= ?', 1.month.ago).count
total_revenue = orders.where('created_at >= ?', 1.month.ago).sum(:total)
report += "Total Orders: #{total_orders}\n"
report += "Total Revenue: $#{total_revenue}\n"
# Send email
UserMailer.monthly_report(self, report).deliver_now
# Log to file
File.open("reports/user_#{id}_#{Date.today}.txt", 'w') do |f|
f.write(report)
end
# Update last report date
update(last_report_generated_at: Time.current)
report
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment