2014-06-24 125 views
0

我想获取过去一天制作的“帖子”的集合,并获取其作者的所有电子邮件。获取与其他模型记录集关联的所有模型记录

author has_many posts

第1步:创建的所有帖子今日(作品)

Posts.where('created_at > :today', today: Time.zone.now.beginning_of_day) 

步骤2:(?究竟是什么语法)获取的所有作者,这些职位

Posts.where('created_at > :today', today: Time.zone.now.beginning_of_day).authors??? # Returns undefined method for ActiveRecord_Relation. 

回答

0

渴望负荷作者

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

你可以得到电子邮件的阵列那样:

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) 
相关问题