我正在寻找使用MongoMapper的Eager Load Associated Documents。假设我的作者有一个:has_one条件的帖子,我应该可以使用单个查询加载作者使用MongoMapper进行关联加载
Post.find(:all, :include => :author)
任何建议?
我正在寻找使用MongoMapper的Eager Load Associated Documents。假设我的作者有一个:has_one条件的帖子,我应该可以使用单个查询加载作者使用MongoMapper进行关联加载
Post.find(:all, :include => :author)
任何建议?
更新:下面的代码就像模型工作流程..我尝试了一些编码后,它没有工作!
可以说你有Post模型和用户模型。
用户has_many帖子,并且您希望所有用户(作者)及其帖子。
这里有一个提示来处理它。我的例子是获取一个帖子。
post.rb
class Post
include MongoMapper::Document
key :title, String
key :body, String
key :user_id, ObjectId
belongs_to :user
end
和user.rb
class User
include MongoMapper::Document
key :name
many :posts, :embed => :title
end
现在,
u = User.first
p = u.posts.first
puts p.title # read it from embedded doc
puts p.body # lazy loading
这里的窍门是嵌入大多是常见的领域,如用户名,_id,用户slu,等
我没有测试以上,但你必须尝试!
最佳 --Amr
关于同一主题,这将是巨大的,如果我能有* *内嵌对象这反过来有关联的数组相同。例如:'question = Question.first; question.comments(:include =>:user)'(注释是一个嵌入式模型)。 – hsribei 2010-10-21 21:00:45