ruby – Mongoid:从父级的嵌入式文档运行回调
发布时间:2020-12-17 02:59:22 所属栏目:百科 来源:网络整理
导读:Rails 3.0.1 Mongoid(2.0.0.beta.20) 班级职位 ?????embeds_many:评论 ?????字段:comments_count ????结束 Class Comment embedded_in :commentable,:inverse_of = :commentsend 我想选择10个评论最多的帖子.要做到这一点,我需要Post中的comments_count字
Rails 3.0.1
Mongoid(2.0.0.beta.20) 班级职位 Class Comment embedded_in :commentable,:inverse_of => :comments end 我想选择10个评论最多的帖子.要做到这一点,我需要Post中的comments_count字段.但由于我的评论是多态的(Post.comments,Message.comments等),我不希望在Post中创建inc回调.我不想做的是在Comment中创建回调,它将更新Post中的comment_count字段. 我不知道如何从parrent文件中在Field上的嵌入式文档中执行inc操作,并从parrent文件执行此回调 解决方法
以下是如何从嵌入式多态注释中增加Post:
Class Comment after_create :update_post_comment_count def update_post_comment_count if self._parent.class == Post Post.collection.update( {'_id' => self._parent._id},{'$inc' => {'comment_count' => 1}} ) end end end 我很确定只要创建一个新的Comment就会执行这个回调,所以我认为你不必担心从父文档执行它.如果有效,请告诉我. 有关嵌入式文档中回调的更多信息,请参见this SO answer和此Github issue. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |