#rubyonrails - Sat 31 Mar 2007 between 06:42 and 06:48

NY Lost Funds



ReinHso classroom.sections gives an array of all sections for that classroom
classroom.sections.collect{|section| section.students}.flatten (I forgot the flatten earlier) gives an array of each section's students
so that's essentially all students in the classroom
because section.students is all students for that section
so you're doing classroom.sections and then for each section you do section.students and then that's shoved into an array
zachinglishttp://pastie.caboo.se/50764 <-- can someone help me on my automagic function please?
PalishOkay, but isn't that like, really expensive?
since rails has to load all the students
ReinHyes. but I'll explain how we optimize in a sec
you do the flatten to turn [ [student, student], [student, student] ] into [student, student, student, student]
(otherwise they're grouped by section)
Now, as you can imagine, this is n+1 queries
and that sucks
zachinglisMorning ReinH, feeling better after that flight?
ReinHhey zachinglis
the :include option in find uses OUTER JOINs to basically use one big query to pull both the records and their dependant records
AnakinKain<%= javascript_tag( image_tag(theme.bgsmall, :size=> "100x100"), "onclick="Element.setStyle('background_main', {background-image: theme.bglarge} )"" %>
ReinHso Section.find(:first, :include => 'students') pulls the section and its students in one query
AnakinKainthis code is causing me an error, and I can't figure out why.
ReinHPalish: with me so far?
zachinglisAnakinKain: your missing a )
Palishsure
zachingliserrr, scrap that
ReinHPalish: so... given a @classroom
and a @student you want to check
AnakinKainok thanks.
ReinH@classroom.sections gets all the sections
zachinglisahhh I see AnakinKain
"onclick="Element.setStyle('backgrou you haven't escaped "s
ReinH@classroom.sections.find(:all, :include => 'students') gets all the sections *and* their students in one query
AnakinKainbut there is only one.
zachinglis"onclick=\"Element.setStyle(\'backgrou ....etc
ReinHThat's how we avoid it being so expensive
zachinglisoh, don't escape 's as you don't need to
but escape the "s
AnakinKainoooo ok.
ReinHPalish: with me?
Palishyes
ReinHPalish: @classroom.sections.find(:all, :include => 'students').collect{|section| section.students }.flatten # gets all the students for each section in that classroom
zachinglisAnakinKain: Basically, it's reading your code and then see's the ", and thinks that's the end

Page: 1 8 15 22 29 36 43 50 57 64 71 78 

IrcArchive

NY Lost Funds