开发者

Rails 3 and mongoid: How would you about sorting / grouping collect to display as a 2 dimension table?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜