Ruby on rails 获取与其他模型关联的所有模型记录';这是一套记录

Ruby on rails 获取与其他模型关联的所有模型记录';这是一套记录,ruby-on-rails,ruby-on-rails-4,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Rails Activerecord,我想收集过去一天的“帖子”,并获取他们所有作者的电子邮件 一个作者有许多帖子 步骤1:获取今天创建的所有帖子(works) 步骤2:获取这些帖子的所有作者(这篇文章的语法是什么?) 渴望加载作者 Posts.includes(:author).where('created_at > :today', today: Time.zone.now.beginning_of_day).collect(&:author) 您可以通过这种方式获取大量电子邮件: Posts.where('cr

我想收集过去一天的“帖子”,并获取他们所有作者的电子邮件

一个
作者有许多帖子

步骤1:获取今天创建的所有帖子(works)

步骤2:获取这些帖子的所有作者(这篇文章的语法是什么?)

渴望加载作者

Posts.includes(:author).where('created_at > :today', today: Time.zone.now.beginning_of_day).collect(&:author)

您可以通过这种方式获取大量电子邮件:

Posts.where('created_at > :today', today: Time.zone.now.beginning_of_day).joins(:author).pluck("authors.email")
如果需要
ActiveRecord::Relation
,则必须使用要获取的类启动查询

Author.joins(:posts).where("posts.created_at > :today", today: Time.zone.now.beginning_of_day)
Posts.where('created_at > :today', today: Time.zone.now.beginning_of_day).joins(:author).pluck("authors.email")
Author.joins(:posts).where("posts.created_at > :today", today: Time.zone.now.beginning_of_day)