ruby – Mongoid:从父级的嵌入式文档运行回调

ruby – Mongoid:从父级的嵌入式文档运行回调,第1张

概述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_ 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字段.但由于我的评论是多态的(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  endend

我很确定只要创建一个新的Comment就会执行这个回调,所以我认为你不必担心从父文档执行它.如果有效,请告诉我.

有关嵌入式文档中回调的更多信息,请参见this SO answer和此Github issue.

总结

以上是内存溢出为你收集整理的ruby – Mongoid:从父级的嵌入式文档运行回调全部内容,希望文章能够帮你解决ruby – Mongoid:从父级的嵌入式文档运行回调所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1288617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存