AlbertEngelB
AlbertEngelB

Reputation: 16436

Mongoid "find" returns nil, when "find_by" retrieves record

I'm using Mongoid to access a MongoDB database, however I'm running into an odd problem. It seems like I can only query for records using find_by, as find will always return nil:

invoices = Invoice.find({})
p "invoices"
p invoices
puts ''

invoice = Invoice.find_by({ _id: <ObjectId> })
p "invoice"
p invoice
puts ''

The second query using find_by will return a single record. According to the documentation, find should be returning every record that satisfies the query.

Does anyone have an idea what could be causing this?

Upvotes: 0

Views: 766

Answers (1)

p80n
p80n

Reputation: 46

Be careful not to confuse the Moped syntax with the Mongoid syntax. For Mongoid, the docs describe the find method:

Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match

If you really want every record, Invoice.all can do the trick. (Also be careful with your find_by method. The Mongoid syntax varies from mongo's a bit, so you don't have to have the curlies around your params.)

Upvotes: 3

Related Questions