Skip to content

Instantly share code, notes, and snippets.

@jacquescrocker
Created July 9, 2010 19:45
Show Gist options
  • Save jacquescrocker/469948 to your computer and use it in GitHub Desktop.
Save jacquescrocker/469948 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
end
class Post
include Mongoid::Document
references_many :comments
end
class Comment
include Mongoid::Document
field :user_id
referenced_in :parent, :inverse_of => :comment
end
I have several users, several posts and for every post, several comments.
Every post has one user. However the user table isn't on mongodb (I could talk with you about why. But please, that's not the question here).
I'm looking forward getting all the comments for one user.
So looping through all the posts to get the comments with user_id = x.
@jacquescrocker
Copy link
Author

now you can do Comment.where(:user_id => params[:user_id]) to return the results

@dmathieu
Copy link

Yeah. But I'd loose all the advantages there is with embedded documents.

@jacquescrocker
Copy link
Author

dmathieu: yep, I agree. I think shingara's query is better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment