Number of ActiveRecord Arrays

I am trying to count # objects in an array using a block, for example:

cc = u.cookies.count {|n| n.opened} 

This is return 3, which is incorrect. I took another step and did the following:

 cc = u.cookies.count {|n| false} 

which should always return 0, but it returns 3 !!!.

This is returning 0, as it should:

 [1,2,3,4].count {|n| false} 

Here is my user model:

 class User < ActiveRecord::Base has_many :cookies end 

What's happening? Thanks

+4
source share
1 answer

u.cookies ActiveRecord::Relation , not an array.

So, whatever the block, the result will not change, you need to do:

 cc = u.cookies.where(:opened => true).count 
+3
source

All Articles