3

I can create a hash like this:

@time_offs_by_date = @time_offs.group_by{ |time_off| time_off.start_date}

Which I then iterate through using

@time_offs_by_date[date] # I am drawing a calendar date-by-date
# I then list details about every object grouped by the specified date

I would like to group my objects by every date included in the range between time_off.start_date and time_off.end_date.

How would I do this?

1 Answer 1

3

This should do it:

@time_offs_by_date = @time_offs.group_by{ |time_off| time_off.start_date..time_off.end_date }

@time_offs_by_date.each do |range, time_offs|
  # do your logic here
end

This will produce a Hash with Range objects as keys, and an array of TimeOff as values.

Documentation about the class Range (Ruby 1.9.3): http://www.ruby-doc.org/core-1.9.3/Range.html

Hope that helps!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.