14

我知道这可以做到:

Article.where("published_at <= ?", Time.now).includes(:comments)

但是,如果我只想获得过去一个月发布的评论怎么办?

.includes 运算符是否允许条件?

4

2 回答 2

12
Article.includes(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month)

编辑:

Article.joins(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month)
于 2011-02-02T16:43:54.557 回答
4

在 Rails4 中,它应该是: Article.includes(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month).references(:comments)

来源

于 2015-12-17T16:48:38.583 回答