Ruby on Rails Using ActiveRecord to Abstract “Greatest N Per Group” Queries My approach allowed me to specify the group based on a set of columns and to abstract the required join logic behind an ActiveRecord scope.