eager-loading


Rails eager loading with conditions


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.

Related Links

How to enable eager loading on a association in phpactiverecord?
Rails eager loading with conditions
Rails eager loading and conditions
Eager loading for globalize2 translations

Categories

HOME
opencart
freeradius
writefile
command-prompt
malware
nsis
qooxdoo
gentelella
capistrano
google-search-console
fasm
match
watch-os-3
bootstrap-switch
vmware-workstation
c#-7.0
office-ui-fabric
varnish-vcl
nose
tfsbuild
significance
conda
generator
candlestick-chart
echosign
ccavenue
npoi
envoy
es6-modules
vcenter
xcrun
epplus
jfxtras
testlink
runtimeexception
django-smart-selects
imgur
slideshow
angular-datatables
opal-framework
elasticsearch-aggregation
docker-ucp
absolute
swift2.3
cloudwatch
nusoap
hibernate-ogm
entropy
spring.net
punctuation
watir-webdriver
mura
portability
dynamics-crm-4
mpmovieplayercontroller
apple-configurator
textblock
mnesia
persistent-object-store
onepage-checkout
voronoi
flask-security
pack
m4
ajax4jsf
grinder
gdl
deeplink
phpquery
netdatacontractserializer
replicaset
nsmutabledata
debug-symbols
maven-shade-plugin
fat32
uno
microsoft-expression-web
thoughtworks-go
xml-signature
multivariate-testing
webautomation
neo4jphp
xulrunner
nodeload
jchartfx
visual-c++-2005
postgres-xc
sunstudio
arel
gprof
jdom
kobold2d
series-40
wcf-callbacks
servercontrol
boost-foreach
vows
getopts
dip
cassini
ning
downloadfile

Resources

Encrypt Message