MongoDB 关于对 mongoid 关系不解

sanvi · 2012年10月17日 · 最后由 orichi 回复于 2012年10月17日 · 3056 次阅读
class Account
has_many :complete_posts, :class_name => "Post", :inverse_of => :complete_user

class Post
belongs_to :complete_user, :class_name => "Account", :inverse_of => :complete_posts

为什么我保存后,用 mongohub 查看 mongodb 在 Post 能查看到 complete_user_id 但在 Account 却看不到 complete_post_ids

代码里面能还能打印出来

@post.complete_user.complete_post_ids
=> ["507e24e272eee588f5000001",
 "50642a30c3666e92b1000006",
 "5064242272eee53b970000a2",
 "506426b172eee53b970000a5",
 "50641c4572eee53b97000043",

而且 self.complete_post_ids.include?(uid) 返回 true

return false if self.complete_post_ids.include?(uid)
    128:   self.push(:complete_post_ids,uid)  

我之前没有保存过进去

inverse_of 这么高级的参数也用上了?英文不行,看了半天也不看懂参数是啥意思,有人能解释一下吗?

#1 楼 @cxh116 http://ruby-china.org/topics/5375 @huacnlee 侧栏的相关话题为什么去掉了 已经在两个帖子里手动推荐相关话题了。。http://ruby-china.org/topics/5528

#2 楼 @hooopo Solr 去掉了,没有 more like this 的功能

#2 楼 @hooopo 看了下你的回答,其实还不是特别的明白,而且如何能解释上面的问题?

#4 楼 @sanivbyfish 答的是 1L 的问题

其实就是指定反向关联的关联名称,has_many 和 belong_to 是双向的,inverse_of 可以省掉一个,直接把另外一个放在后边,你再检测一下 model 关系,问题应该不大

我觉得对has_many这种关系,Account没必要保存所有complete_post_ids,这样既造成数据冗余,又容易造成数据不一致,还失去了查询的灵活性。需要Account#complete_posts时,只要一个简单的查询就能找到所有的posts。

Without :inverse_of m and f.man would be different instances of the same object (f.man being pulled from the database again). 貌似就看到一句有用的

看起来这个参数是在 has_one 里节省内存用的

需要 登录 后方可回复, 如果你还没有账号请 注册新账号