【问题标题】:How would I grab only articles with comments that were created 20 minutes ago?我如何只抓取 20 分钟前创建的评论文章?
【发布时间】:2012-05-15 04:44:09
【问题描述】:

使用 mongodb 和 mongoid。一个人怎么会抓住所有的文章。只有 20 分钟前创建的带有 cmets 的文章?

class Article
  include Mongoid::Document

  has_many :comments
end

class Comment
  include Mongoid::Document

  belongs_to :article
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid


    【解决方案1】:

    你也可以这样做,应该更高效:

    articles = Article.where(:"comments.created_at".gt => 20.minutes.ago)
    

    【讨论】:

    • 对于引用关系,我认为这行不通。虽然我不明白为什么这是公认的答案。
    【解决方案2】:
    articles = Article.where(:_id.in => Comment.where(:created_at => 20.minutes.ago).map(&:article_id))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 2019-05-08
      • 2023-02-24
      • 2016-11-14
      相关资源
      最近更新 更多