How does find work in MongoModel?

35 views
Skip to first unread message

Jim S

unread,
Jul 18, 2012, 5:34:20 PM7/18/12
to mongo...@googlegroups.com
I'm pretty new to Ruby and Mongo and I'm having an issue using MongoModel

Here is some code I have created. https://codetique.com/LGko0F7V (you can click any line to leave a comment)

I'm trying to use mongo to store my user data. The authorize route should only create a new user entry if it can't find one already in the database. The problem is that I must not be using find correctly because it never seems to find a pre-existing user entry and creates a duplicate. I looked at the example you have on the site but it doesn't make it clear to me how I search my entries.

In the mongo console I would do something like this

db.users.find({email: "f...@bar.com"});

That doesn't seem to work in Mongo Model. I have tried both User.find(email) and User.find({:email => email})

Could you please advise me on what I'm doing wrong?

James Brooks

unread,
Jul 18, 2012, 5:40:25 PM7/18/12
to mongo...@googlegroups.com
Hi Jim,

I don't have anything infront of me right now to verify what I'll suggest now works, but I'd recommend giving something like the following a shot:

  * Change #find to #where
  OR
  * Change that line to: User.find_by_email(email)


Cheers,
  James

Jim S

unread,
Jul 18, 2012, 6:08:50 PM7/18/12
to mongo...@googlegroups.com
Hi James,

Thanks for the quick response. Both those solutions worked for me. Just out of curiosity are there any more examples of how to use MongoModel other than what's on the official site?

Again thank you for the quick response.

Jim...

James Brooks

unread,
Jul 18, 2012, 6:11:38 PM7/18/12
to mongo...@googlegroups.com
Great to hear!, and you're welcome :)

I don't know of any other documentation for MongoModel, though the interface it uses aims to be identical to ActiveRecord (apart from where it doesn't make sense to), a large amount of things that will work using AR will work using MM (AR has lots of documentation, have a look here to get started: http://apidock.com/rails/ActiveRecord/Base).


Cheers,
  James :)
Reply all
Reply to author
Forward
0 new messages