MongoID find or find_by

Leopd picture Leopd · Jul 10, 2012 · Viewed 10.5k times · Source

The MongoID docs seem to be pretty clear that I should be able to run this and have it work:

Band.find_by(name: "Photek")

but at least with MongoID 2.4.11 this gives me a NoMethodError.

This, on the other hand, works:

Band.find(name: "Photek")

It's easy enough to change find_by to find, but I'm confused what's going on. Is this a case where my gem version is behind the docs, or what?

Answer

user24359 picture user24359 · Jul 10, 2012

The Mongoid docs now show you version 3 by default (it's an RC). You want to look at the docs here for Mongoid 2.

On find vs find_by, this is from the upgrade guide for Mongoid 2 -> 3:

Model.find and model.relation.find now only take a single or multiple ids. Model.first, Model.last also no longer take arguments. For these use Model.find_by instead.

IOW:

Mongoid 2:

Band.find(some_id)
Band.find(name: "Billy Talent")

Mongoid 3:

Band.find(some_id)
Band.find(some_id, some_other_id)
Band.find_by(name: "Billy Talent")