Rails 3 and mongoid: how are you going to sort / group the collection to display as a table with two sizes?

I have a user model with many fields, and I would like to show the table as a matrix of two fields: - created in - type For created_at, I just used group_by like this:

(User.where(:type => "blabla" ).all.group_by { |item| 
item.send(:created_at).strftime("%Y-%m-%d") }).sort.each do | 
creation_date, users| 

This gives me a good array of all users in the creation_date file, so the lines on my table are fine. However, I want to display several rows, each of which is a subset of users for each type. Therefore, at the moment I am doing one query per line (by type, simply replacing "blabla").

This is normal at the moment, because I have only a few types, but this number will increase a lot more soon, and I'm not afraid.

Any suggestion on how I could achieve my expected results?

Thank,

Alex

+3
source share
1 answer

The general answer here is to execute Map / Reduce . As a rule, you do not perform map reduction in real time due to performance limitations. Instead, you run a scheduled map reduction and directly request results.

Here is a map-reduce primer for Ruby . Here is another example using Mongoid.

+4
source

All Articles