Hi all, I submitted a patch a couple of days ago that makes it possible to group on multiple fields when using AR calculations (http:// dev.rubyonrails.org/ticket/10771) Given this table for a Sample model: id year month value 1 2007 3 1 2 2007 3 2 3 2007 4 4 4 2007 4 10 5 2007 5 12 6 2007 5 3 7 2007 5 1 8 2008 1 3 Sample.count(:id, :group => [:year, :month]) and Sample.count(:id, :group => ''year, month'') currently fail. The patch makes both work, returning a nested structure like this: [[ 2007, [[3,2], [4,2], [5,3]] ], [ 2008, [[1,1]]]] (the order of nesting is controller by the order of the fields in the group param, of course). I''d love feedback on the implementation. Thanks! Ben --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
This seems so obvious that I gotta believe its somewhere in the code already. If it is not, a definite +1 for the idea and I will try to test your code. On Jan 13, 1:42 pm, "bscofi...@gmail.com" <bscofi...@gmail.com> wrote:> Hi all, > > I submitted a patch a couple of days ago that makes it possible to > group on multiple fields when using AR calculations (http:// > dev.rubyonrails.org/ticket/10771) > > Given this table for a Sample model: > > id year month value > 1 2007 3 1 > 2 2007 3 2 > 3 2007 4 4 > 4 2007 4 10 > 5 2007 5 12 > 6 2007 5 3 > 7 2007 5 1 > 8 2008 1 3 > > Sample.count(:id, :group => [:year, :month]) and > Sample.count(:id, :group => ''year, month'') currently fail. The patch > makes both work, returning a nested structure like this: > [[ 2007, [[3,2], [4,2], [5,3]] ], [ 2008, [[1,1]]]] > > (the order of nesting is controller by the order of the fields in the > group param, of course). > > I''d love feedback on the implementation. > > Thanks! > Ben--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 13, 3:42 pm, "bscofi...@gmail.com" <bscofi...@gmail.com> wrote:> Hi all, > > I submitted a patch a couple of days ago that makes it possible to > group on multiple fields when using AR calculations (http:// > dev.rubyonrails.org/ticket/10771) > > Given this table for a Sample model: > > id year month value > 1 2007 3 1 > 2 2007 3 2 > 3 2007 4 4 > 4 2007 4 10 > 5 2007 5 12 > 6 2007 5 3 > 7 2007 5 1 > 8 2008 1 3 > > Sample.count(:id, :group => [:year, :month]) and > Sample.count(:id, :group => ''year, month'') currently fail. The patch > makes both work, returning a nested structure like this: > [[ 2007, [[3,2], [4,2], [5,3]] ], [ 2008, [[1,1]]]] > > (the order of nesting is controller by the order of the fields in the > group param, of course). > > I''d love feedback on the implementation.I like the idea, but might suggest returning nested hashes instead. I find them easier to work with. Is that doable? Something like: [ 2007 => { 3=>2, 4=>2, 5=> 3 }, 2008 => { 1=>1 } ] Just a thought. Jeff --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---