Skip to content

Commit 277c714

Browse files
committed
Use ActiveRecord over Arrays for email lists
Signed-off-by: jonathan.kerr <3410350+jonodrew@users.noreply.github.com> #member_emails previously processed an enormous array of objects in-memory. I've altered both that method and the underlying model to work solely in ActiveRecord, thereby reducing the memory strain.
1 parent 71fd0cd commit 277c714

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

app/controllers/admin/chapters_controller.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@ def set_chapter
6767
end
6868

6969
def member_emails(chapter, type)
70-
return chapter.send(type).map(&:email).join("\n") if %w[students coaches].include?(type)
71-
72-
chapter.members.pluck(:email).uniq.join("\n")
70+
members =
71+
case type
72+
when "students"
73+
chapter.students
74+
when "coaches"
75+
chapter.coaches
76+
else
77+
chapter.members
78+
end
79+
members.distinct.pluck(:email).join("\n")
7380
end
7481
end

app/models/chapter.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ def organisers
3434
end
3535

3636
def students
37-
members.select(&:student?)
37+
Member.joins(:groups)
38+
.merge(Group.students)
39+
.distinct
3840
end
3941

4042
def coaches
41-
members.select(&:coach?)
43+
Member.joins(:groups)
44+
.merge(Group.coaches)
45+
.distinct
4246
end
4347

4448
private

0 commit comments

Comments
 (0)