views:

201

answers:

1

I've implemented the following scope in a rails 3 application:

scope :popular, lambda { |l = 5| order('views desc').limit(l) }

However, it seems that when you attempt to count its records directly it doesn't apply the scope filters.

For example:

Post.popular.size #=> 20

Checking the log, it executes the following query:

SQL (0.4ms)  SELECT COUNT(*) AS count_id FROM `posts` LIMIT 5

Now if I execute

Post.popular.all.size #=> 5

And the correct query is executed:

Post Load (1.5ms)  SELECT `posts`.* FROM `posts` ORDER BY views desc LIMIT 5

Anyone else experienced this kind of behavior? If so, any idea if this is the expected behavior or am I facing a bug?

Best regards, DBA