How to group dates according to the month? Ruby + MongoID

-1

I have a class that contains events and their start dates. I need to retrieve all records by grouping them so that the first few days are listed at a time. Example:

2014-01-01, 2014-02-01, 2014-03-01, 2014-01-02, 2014-02-02, 2014-03-02, ...

I need these results, to set up the structure of a calendar. Any help is welcome.

Follow the class.

class Headline
  include Mongoid::Document
  include Mongoid::Timestamps

  field :start_date, type: Date
  field :end_date, type: Date

end
    
asked by anonymous 28.08.2014 / 16:56

2 answers

0

You can set a scope and from it return the objects sorted by start date, as an example:

MongoID

MongoID
# Provide the sorting options.
Person.order_by([[:first_name, :asc], [:last_name, :desc]])

or:

scope :start_date, :order => (:start_date, :asc)
    
28.08.2014 / 19:31
0

To group and sort ruby objects:

headlines.group_by { |h| h.start_date.day }
  .values
  .map { |hs|
    hs.sort_by { |h| h.start_date }
  }

If the idea is to delegate grouping and ordering responsibility to the database query, such an approach to MongoDB might be useful:

    
07.04.2015 / 21:52