Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Group with ranges for ActiveRecord
Igor Kasyanchuk
Igor Kasyanchuk

Posted on

     

Group with ranges for ActiveRecord

If you need to query your Active Record model and group by ranges or multiple values for a specific field - you can try my own gem:

https://github.com/igorkasyanchuk/calculate_in_group

Let's say you have task to count how many Users you have who have 1-5, 5-10, 10+ projects. Solution is simple:

User.calculate_in_group:count,:projects_count,[0,1..5,5..10,10..100,100..]# => {"0"=>555, "1..5"=>145, "10..100"=>3991, "100.."=>190, "5..10"=>2824}
Enter fullscreen modeExit fullscreen mode

More Examples:

# Grouping can be used with :count, :average, :sum, :maximum, :minimum.# Group with RangesUser.calculate_in_group(:count,:age,[...10,10...50,50..]# => {"...10"=>1, "10...50"=>3, "50.."=>3}User.calculate_in_group(:count,:created_at,{"old"=>12.hours.ago..1.minutes.ago,"new"=>Time.now..10.hours.from_now})# => {"old" => 2, "new" => 1}User.calculate_in_group:count,:projects_count,[0,1..5,5..10,10..100,100..]# => {"0"=>555, "1..5"=>145, "10..100"=>3991, "100.."=>190, "5..10"=>2824}# Group with arrays or just valuesUser.calculate_in_group(:count,:role,"with_permissions"=>["admin","moderator"],"no_permissions"=>"user")# => {"with_permissions" => 3, "no_permissions" => 3}# Other agg functionsUser.calculate_in_group(:average,:age,"young"=>0..25,"old"=>60..100)# => {"young" => 11.0, "old" => 80.0}User.calculate_in_group(:average,:age,"young"=>0..25,"old"=>60...100)# => {"young" => 11.0, "old" => 60.0}User.calculate_in_group(:maximum,:age,"young"=>0..25,"old"=>60..100)# => {"young" => 20, "old" => 100}User.calculate_in_group(:minimum,:age,"young"=>0..25,"old"=>60..100)# => {"young" => 3, "old" => 60}User.calculate_in_group(:sum,:age,"young"=>0..25,"old"=>60..100)# => {"young" => 33, "old" => 160}User.calculate_in_group(:sum,:age,{"young"=>0..25,"old"=>60..100})# => {"young" => 33, "old" => 160}# You can specify "other values" (with custom label) which are out of rangesUser.calculate_in_group(:count,:age,{"young"=>10,"average"=>25,"old"=>60},{include_nil:"OTHER"})# => {"young" => 1, "old" => 1, "OTHER" => 7}# You can specify default value for keys which are missing in queryUser.calculate_in_group(:count,:age,{"young"=>10,"average"=>25,"old"=>60},{default_for_missing:0})# => {"young" => 1, "old" => 1, "average" => 0}
Enter fullscreen modeExit fullscreen mode

Look forward hearing your feedback :)

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Working with Ruby on Rails since v.1.2.3. Created dozens of popular open-source projects with 5K+ stars total.
  • Location
    Ukraine
  • Joined

More fromIgor Kasyanchuk

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp