我知道这可以做到:
Article.where("published_at <= ?", Time.now).includes(:comments)
但是,如果我只想获得过去一个月发布的评论怎么办?
.includes 运算符是否允许条件?
我知道这可以做到:
Article.where("published_at <= ?", Time.now).includes(:comments)
但是,如果我只想获得过去一个月发布的评论怎么办?
.includes 运算符是否允许条件?
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)
在 Rails4 中,它应该是:
Article.includes(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month).references(:comments)