Following 2 statements look identical to me, however the first one returns only one object but the second one returns correctly an array of 4 objects.
What is happening here is simple, I get all the Grade objects (1st one using Grade.find(:all) and second one using Grade.all), and then filter out all the grade objects where they have worksheets assigned to them.
I don’t have rails in front of me, but looks like there may be a problem with implied parentheses. Have you tried things like:
@grades = Grade.all*() ** {|grade| Worksheet.find(:all,:conditions=> [‘grade_id =?’,grade.id]).count > 0}*
Again I can’t test so not sure if that helps
Following 2 statements look identical to me, however the first one returns only one object but the second one returns correctly an array of 4 objects.
What is happening here is simple, I get all the Grade objects (1st one using Grade.find(:all) and second one using Grade.all), and then filter out all the grade objects where they have worksheets assigned to them.
in the first statement, find(:all) acts like Enumerable#find when passed a block. Each of the found objects is passed to the block and the first one that returns a truthy value is returned.
in the second statement, all doesn’t call the block so you get exactly the same results you would have without a block at all.
If this is an operation you do a lot in your app, you may want to check out the :counter_cache option of belongs_to, which will allow you to write this (correctly) as:
@grades = Grade.where(‘worksheets_count > 0’).all
Also note that if counts are all you’re looking for, you should avoid doing a find(:all) on them (as on Worksheet above); the count method on the model class can do this much more efficiently (with a SQL COUNT statement, rather than loading a bunch of objects and then counting them).
Thanks Matt, Your alternative method on how to best get the data definitely is a much optimized solution then what I was using, I am not using that and everything works great.
However, I am still confused on one part.
as per my understanding
Grade.find(:all)
and
**Grade.all **
both should return an array of Grade objects, so how are the results different if I just pass the objects through this block.