Skip to content

Instantly share code, notes, and snippets.

@iyanski
Created March 30, 2012 16:49
Show Gist options
  • Save iyanski/2252788 to your computer and use it in GitHub Desktop.
Save iyanski/2252788 to your computer and use it in GitHub Desktop.
Counting results when specifying limit() in mongoid now works
1.9.2p290 :022 > Photo.all.count
=> 21
1.9.2p290 :023 > Photo.where(orientation: 'portrait').limit(2).count
=> 9
1.9.2p290 :024 > Photo.where(orientation: 'portrait').limit(2).count(true)
=> 2
1.9.2p290 :025 >
@rgaufman
Copy link

rgaufman commented Apr 20, 2018

Doesn't seem to be :(

s(test)> self.where(agent: agent).limit(5).count(true)
NoMethodError: undefined method `[]' for true:TrueClass
from /usr/local/lib/ruby/gems/2.6.0/gems/mongo-2.5.1/lib/mongo/collection/view/readable.rb:131:in `count'

@rgaufman
Copy link

rgaufman commented May 1, 2018

This page has the answer! < https://stackoverflow.com/questions/16074404/mongoid-limit-does-not-work-in-mongoid-3-1-x

It's basically:

@events.count(limit: 50)

@ahmadpriatama
Copy link

@rgaufman thanks, this save me alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment