我试图通过一个属性 Select 对象,使用@videos.uniq{|p| p.author}
time = Time.new(2014, 12)
start_time = time.beginning_of_month
end_time = time.end_of_month
videos = Video.where("created_at > ? AND created_at < ?", start_time, end_time).where("likes > ?", 15)
selected_videos = videos.uniq{|p| p.author}
puts videos.count, videos.class, selected_videos.count
#=> 23, Video::ActiveRecord_Relation, 23
videos_first = videos.first(23)
selected_videos = videos_first.uniq{|p| p.author}
puts videos_first.count, videos_first.class, selected_videos.count
#=> 23, array, 10
.uniq
不代表ActiveRecord_Relation
.问题是查询返回Video::ActiveRecord_Relation
,但我需要array
.
当然,这可以通过使用to_a
来实现,但这是优雅的吗?
- 正确的处理方法是什么?
- 可以用
.uniq
换activerecord:relation
吗?