Leopd
Leopd

Reputation: 42747

MongoID find or find_by

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?

Upvotes: 16

Views: 12264

Answers (1)

user24359
user24359

Reputation:

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")

Upvotes: 18

Related Questions