I have an array of Assignment objects that I create from a database call:
@assignments = @player.assignments
I want to count them with this:
@assignments.count {|x| x.sets == 0.0}
This should count the number of assignments with 0.0 sets. However, this always returns the total number of objects in @assignments. I have checked that
@assignments.each {|x| puts x.sets == 0.0}
does not return true in all the cases. Any clues?
Edit>
@assignments.map(&:sets)
=> [35.0, 120.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.0, 75.0, 0.0, 0.0, 0.0, 0.0]
Most likely, @assignments
is not an Array
instance, but ActiveRecord::Relation
, try @assignments.class
. If so, count
method does not work as you expected, it returns numbers of records.
Try
@assignments.where(sets: 0).count
Or even (convert to array first)
@assignments.to_a.count {|x| x.sets == 0.0}
Try with this:
@assignments.select {|x| x if x.sets == 0.0}.count
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With