How would I grab only articles with comments that

2020-03-30 10:11发布

Using mongodb and mongoid. How would one grab all articles. Only articles with comments created 20 minutes ago?

class Article
  include Mongoid::Document

  has_many :comments
end

class Comment
  include Mongoid::Document

  belongs_to :article
end

2条回答
【Aperson】
2楼-- · 2020-03-30 10:21

you can also do this, which should be more efficient:

articles = Article.where(:"comments.created_at".gt => 20.minutes.ago)
查看更多
We Are One
3楼-- · 2020-03-30 10:24
articles = Article.where(:_id.in => Comment.where(:created_at => 20.minutes.ago).map(&:article_id))
查看更多
登录 后发表回答