Last active
November 20, 2025 13:17
-
-
Save akapitula/e7581334a3c43d3b12f0d56ff294d29c 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 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