1

I have an association between Students and Attendances and I would like to query for all students and associated attendance records for a particular date range, including students that don't have any attendance records in that date range (e.g. to build an attendance table for a particular week).

class Student < ActiveRecord::Base
  has_many :attendances
  scope :with_attendances_for_week_of, lambda { |d|
    includes(:attendances).joins("LEFT OUTER JOIN attendances ON (student.id = attendances.student_id AND attendances.date BETWEEN #{(d-d.cwday+1)} AND #{(d-d.cwday+7)})")
  }
end

class Attendance < ActiveRecord::Base
  belongs_to :student
  scope :for_week_of, lambda {|d| where(:date => (d-d.cwday+1)..(d-d.cwday+7)}
end

What I'd like is a way to accomplish something like:

Student.with_attendances_for_week_of(params[:date) do |student|
  display_attendance_row student.name, student.attendances
end

I seem to be stuck choosing between solutions that query for ALL past attendance records, or N+1 solutions (where I query student.attendances.for_week_of(params[:date]) for each student).

Simpler scopes like the following only returned students that have attendance records in the date range.

scope :with_attendances_for_date_range, lambda { |date_range|
  includes(:attendances).where(:attendances => {:date => date_range})
}

At this point I'd take any solution that doesn't lead to N+1 or querying for thousands of unneeded Attendance records.

anowell
  • 231
  • 2
  • 7
  • 1
    [This](http://stackoverflow.com/questions/9642900/eager-loading-the-right-way-to-do-things/12089216#12089216) may help you. I use that workaround to handle the cases where I need the eager-loaded associations to be filtered based on dynamic conditions. – Anjan Aug 23 '12 at 10:05
  • Thanks. I ended up doing something similar back when I asked, but I didn't realize I could reassign the association(:item).target so I just used a helper to filter the associated items. Reassigning the association target in the model looks cleaner. – anowell Feb 28 '13 at 23:23

0 Answers0