Passing date range parameters to SQL/ActiveRecord query in Rails
<p>Here I have an Order and an OrderReport table. I am doing a join on it extracting some data from each of the tables. </p>

<pre><code> OrderReport.joins("INNER JOIN orders on orders.id = order_reports.for_object_id and order_reports.for_object_type = 'Order'").

group("order_reports.sales_user_id, EXTRACT(MONTH from event_at), orders.user_id,event_at").

pluck("order_reports.sales_user_id, EXTRACT(MONTH from event_at), orders.user_id,sum((cached_user_total_due - cached_sales_tax) * split_percentage), min(event_at)")
</code></pre>

<p>I have this very long sql + ActiveRecord query. I wanted to add an initializer and have it take in a date start and date end to make it a little more flexible for being able to pull reports from the previous year going forward. </p>

<p>The start and end date would be coming from the Order table which has an event_at column. I know I would start off by doing this: </p>

<pre><code>  attr_reader :start_date, :end_date

  def initialize(start_date:, end_date:)
    @start_date = start_date
    @end_date = end_date 
  end
</code></pre>