sam*_*tte 14 activerecord ruby-on-rails ruby-on-rails-3
我知道这可以做到:
Article.where("published_at <= ?", Time.now).includes(:comments)
Run Code Online (Sandbox Code Playgroud)
但是,如果我只想在过去一个月发布评论怎么办?
.includes运算符是否允许条件?
Reu*_*aby 12
Article.includes(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month)
Run Code Online (Sandbox Code Playgroud)
编辑:
Article.joins(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month)
Run Code Online (Sandbox Code Playgroud)