Get data from two collection with Rails 3.1 and Mongoid
I have two models called Post and User, Post belongs_to user and U开发者_如何学Cser has_many posts. When I list all posts I also want the user username and email to be included. I a relational database I would use join but how can I do this with Mongoid?
What you'll want to do is eager load your User
model when querying for your posts. You can do this using the includes
method.
Using your example above, you could do something like:
Post.includes(:user).all
精彩评论