On Thursday, 19 April 2012 22:14:51 UTC-4, Dheeraj Kumar wrote:
The find method takes the option hash as the last parameter. Either use Model.all(options) or Model.find(:all, options)
The correct code is:
@grades = Grade.find(:all, {|grade| Worksheet.find(:all,:conditions=> ['grade_id =?',grade.id]).count > 0})
Um, no. That won't even parse.
On Friday 20 April 2012 at 7:38 AM, Tyler wrote:
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
On Thursday, April 19, 2012 2:03:05 PM UTC-7, Ankur Jain wrote:
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.
@grades = Grade.find(:all){|grade| Worksheet.find(:all,:conditions=> ['grade_id =?',grade.id]).count > 0}
@grades = Grade.all {|grade| Worksheet.find(:all,:conditions=> ['grade_id =?',grade.id]).count > 0}
There are two things going on here:
- 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).
--Matt Jones