I have User model with many fields and I would like to display a table as a matrix of 2 of those fields: - created_at - type For the created_at I simply used a group_by as so:
(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 nice array of all the users per creation_date, so the lines o开发者_StackOverflow中文版n my table are ok. However I want to display multiple lines, each representing the sub selection of the users per type. So for the moment, I am performing one request per line (per type, simply replacing the "blabla").
For the moment it's ok because I have just a few type, but this number will soon increase a lot more, and at this will not be efficient I am afraid.
Any suggestion on how I could achieve my expected results ?
Thanks,
Alex
The general answer here is to perform a Map / Reduce. Generally, you do not perform the map-reduce in real time due to performance constraints. Instead you run the map-reduce on a schedule and query against the results directly.
Here's a primer on map-reduce for Ruby. Here's another example using Mongoid specifically.
精彩评论