-
Notifications
You must be signed in to change notification settings - Fork 5.5k
How To: Embed users in your account model with Mongoid
If you have multiple accounts in your system, each with many users, and you’re using a document-based database like MongoDB, it makes perfect sense to embed your users within your account model, so that they’re not all just sitting at the top level. This can be quite tricky to do with Devise, since Warden will call User#find
to retrieve the stored user model.
This gist shows what you need to do to your User model to get this to work. The main change is to User#find
, which translates its parameters into a call to Account#find
instead, then returns the relevant user.
Note that you can’t just use Account.find :first, :user => options[:conditions]
, because, according to the MongoDB docs, you need to match all fields on a subobject, whereas dot notation will let you get away with partial matches.