From 8376bb57dd0da389a8be10a41e1277b316e3089b Mon Sep 17 00:00:00 2001 From: Matt Pruitt Date: Mon, 2 Mar 2015 22:37:07 -0800 Subject: [PATCH 1/2] Eager load gardens on guide reindex. --- app/models/guide.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/guide.rb b/app/models/guide.rb index 7709382f5..bec337fcd 100644 --- a/app/models/guide.rb +++ b/app/models/guide.rb @@ -70,7 +70,7 @@ def compatibilities @compatibilities = [] - User.each do |user| + User.includes(:gardens).each do |user| @compatibilities << { user_id: user.id.to_s, score: compatibility_score(user).to_i } From 87dda23d9deb6410796f5baab65a8499128f3fde Mon Sep 17 00:00:00 2001 From: Matt Pruitt Date: Mon, 2 Mar 2015 22:59:29 -0800 Subject: [PATCH 2/2] Don't run full guide reindex on garden save. Reindex each guide individually in order of popularity. --- app/jobs/reindex_guides_job.rb | 4 +++- spec/jobs/reindex_guides_job_spec.rb | 4 +++- spec/models/garden_crop_spec.rb | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/jobs/reindex_guides_job.rb b/app/jobs/reindex_guides_job.rb index ebc3e51d8..4358823d0 100644 --- a/app/jobs/reindex_guides_job.rb +++ b/app/jobs/reindex_guides_job.rb @@ -2,6 +2,8 @@ class ReindexGuidesJob < ActiveJob::Base queue_as :default def perform - Guide.reindex + Guide.desc(:popularity_score).each do |guide| + guide.reindex_async + end end end diff --git a/spec/jobs/reindex_guides_job_spec.rb b/spec/jobs/reindex_guides_job_spec.rb index fbe4af015..fdcd41d0d 100644 --- a/spec/jobs/reindex_guides_job_spec.rb +++ b/spec/jobs/reindex_guides_job_spec.rb @@ -2,7 +2,9 @@ describe ReindexGuidesJob do it 'reindexes guides' do - expect(Guide).to receive(:reindex) + FactoryGirl.create(:guide) + + expect_any_instance_of(Guide).to receive(:reindex_async) ReindexGuidesJob.new.perform end diff --git a/spec/models/garden_crop_spec.rb b/spec/models/garden_crop_spec.rb index 053e4d66f..195e62b93 100644 --- a/spec/models/garden_crop_spec.rb +++ b/spec/models/garden_crop_spec.rb @@ -14,7 +14,9 @@ end it 'reindexes guides' do - expect(Guide).to receive(:reindex) + FactoryGirl.create(:guide) + + expect_any_instance_of(Guide).to receive(:reindex_async) FactoryGirl.create(:garden, user: FactoryGirl.create(:user)) end