Skip to content

Commit 0ed3699

Browse files
committed
fix: workshop models workshop sponsors with host query
The previous implementation was loading workshops upfront which was causing slow queries which timed out the app. This new query should load only the related workshop sponsors to a workshop in one sql query
1 parent 8776650 commit 0ed3699

File tree

3 files changed

+5
-31
lines changed

3 files changed

+5
-31
lines changed

app/models/workshop.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class Workshop < ApplicationRecord
3030
before_validation :set_date_and_time, :set_end_date_and_time, if: proc { |model| model.chapter_id.present? }
3131
before_validation :set_opens_at
3232

33+
def workshop_sponsors_with_host_true
34+
workshop_sponsors.where(host: true)
35+
end
36+
3337
def host
34-
WorkshopSponsor.hosts.for_workshop(id).first&.sponsor
38+
workshop_sponsors_with_host_true.first&.sponsor
3539
end
3640

3741
def waiting_list

app/models/workshop_sponsor.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@ class WorkshopSponsor < ApplicationRecord
33
belongs_to :workshop
44

55
validates :sponsor_id, uniqueness: { scope: :workshop_id, message: :already_sponsoring }
6-
7-
scope :hosts, -> { where('workshop_sponsors.host = ?', true) }
8-
scope :for_workshop, ->(workshop_id) { where('workshop_sponsors.workshop_id = ?', workshop_id) }
96
end

spec/models/workshop_sponsor_spec.rb

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,4 @@
66
.with_message('already a sponsor')
77
end
88
end
9-
10-
context '#scopes' do
11-
context '#hosts' do
12-
it 'includes workshops with hosts' do
13-
workshop_sponsor = Fabricate(:workshop_sponsor, host: true)
14-
15-
expect(WorkshopSponsor.hosts).to include(workshop_sponsor)
16-
end
17-
18-
it 'excludes workshops without hosts' do
19-
expect(WorkshopSponsor.hosts).to eq []
20-
end
21-
end
22-
23-
context '#for_workshop' do
24-
it 'includes sponsors of the workshop' do
25-
workshop_sponsor = Fabricate(:workshop_sponsor)
26-
expect(WorkshopSponsor.for_workshop(workshop_sponsor.workshop)).to include(workshop_sponsor)
27-
end
28-
29-
it 'excludes sponsors not sponsoring the workshop' do
30-
Fabricate(:workshop_sponsor)
31-
workshop = Fabricate(:workshop_no_sponsor)
32-
expect(WorkshopSponsor.for_workshop(workshop)).to eq []
33-
end
34-
end
35-
end
369
end

0 commit comments

Comments
 (0)