开发者

Rails 3 ActiveRecord conditional includes?

开发者 https://www.devze.com 2023-02-08 17:21 出处:网络
I know this can be done: Article.where(\"published_at <= ?\", Time.now).includes(:comments) But w开发者_开发知识库hat if I\'d like to only get comments posted in the past month?

I know this can be done:

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

But w开发者_开发知识库hat if I'd like to only get comments posted in the past month?

Does the .includes operator allow conditions?


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

EDIT:

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


In Rails4, it should be: Article.includes(:comments).where("articles.published_at <= ? and comments.created_at >= ?", Time.now, Time.now - 1.month).references(:comments)

Source

0

精彩评论

暂无评论...
验证码 换一张
取 消