Q: Storing old previous year data in Rails? I'm developing an app which has massive data entries.
Its like Campaign which has attrs like rate_per_sq_feet, start_date, end_date. i.e it will have max date of around 30 days.
Once the campaign is finished, its done and another starts.
Now I'm confused that how to store those campaigns as reports so that its not accessed regurlarly. What I mean is to store in such a way that it will act like report on later years to come?
Its something like fiscal year on accounts where the previous year reports are stored with all the calculations done so that when retrieved later, all the algorithms and calculations shouldn't be performed. Something like frozen data??

A: You can store the campaign reports in the database OR file system. The report can be generated upon the first request for the archived campaign.
class Campaign < ActiveRecord::Base   
  # Campaign model has an attribute called report_file
  def report_file
    return nil unless expired?
    attributes['report_file'] ||= generate_report_file
  end

  def generate_report_file
    return nil if attributes['report_file']
    # Generate the report using Prawn PDF OR wickedPDF etc.
    # Update report_file attribute with the report file location
    # Return the file location
  end
end

class CampaignsController < ApplicationController
  before_filter :check_expiry, :only => :show
  def report
    if @campaign.report_file
      send_file(@campaign.report_file)
    else
      # handle error
    end
  end
  def show
  end
  def check_expiry
    @campaign = Campaign.find(params[:id])
    if @campaign.expired?
      render :report
    end
  end
end

