1

I have a notifications table where I email out reports to people based on the frequency they've selected.

If an email address exists across multiple questions and has the same frequency...then I need to group them together so I can send one report for both questions.

[
   #<Notification id: 1, question_id: 58, email: "[email protected]", frequency: "daily">, 
   #<Notification id: 2, question_id: 25, email: "[email protected]", frequency: "daily">,
   #<Notification id: 3, question_id: 47, email: "[email protected]", frequency: "monthly">,
   #<Notification id: 3, question_id: 19, email: "[email protected]", frequency: "monthly">
] 

So in my example data, 3 reports would be sent:

I may not be explaining this very well, so let me know if something needs clarification.

2
  • how does notification fit with your other models? Commented Oct 10, 2013 at 16:08
  • Question has_many Notifications and Notification belongs_to Question Commented Oct 10, 2013 at 16:10

1 Answer 1

1

You can achieve this with a regular group_by:

@notifications = your_method_to_retrieve_notifications
@notifications = @noticications.group_by do |notif|
  notif.ferquency + ':' + notif.email
end

This will group your notifications like this:

@notifications = {
  'daily:[email protected]' => [<Notification id: 1>, #etc.],
  'monthly:[email protected]' => [# list of notifications],
  'monthly:[email protected]' => [# list of notif]
}

If you want only an array of list of notifications grouped by frequency & email, use the above method and add this:

@notifications = your_method_to_retrieve_notifications
@notifications = @noticications.group_by do |notif|
  notif.ferquency + ':' + notif.email
end.values
# returns Array of arrays like:
[
  [<Notification id: 1 frequency: "daily", email "[email protected]">,<Notification id: 2 frequency: "daily", email "[email protected]">],
  [<Notification id: 3 frequency: "daily", email "[email protected]">],
  [<Notification id: 4 frequency: "monthly", email "[email protected]">,<Notification id: 5 frequency: "monthly", email "[email protected]">],
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.