Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from mezis/avoid-n-plus-one-queries
Browse files Browse the repository at this point in the history
Avoid N+1 queries
  • Loading branch information
mezis committed Dec 2, 2013
2 parents 8b2e412 + 6fe9298 commit 4bc7dce
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 2 additions & 4 deletions lib/fuzzily/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def _matches_for_trigrams(trigrams)
scoped(:select => 'owner_id, owner_type, count(*) AS matches, MAX(score) AS score').
scoped(:group => 'owner_id, owner_type').
scoped(:order => 'matches DESC, score ASC').
with_trigram(trigrams).
map(&:owner)
with_trigram(trigrams)
end

def _add_fuzzy_scopes
Expand All @@ -59,8 +58,7 @@ def _matches_for_trigrams(trigrams)
select('owner_id, owner_type, count(*) AS matches, MAX(score) AS score').
group('owner_id, owner_type').
order('matches DESC, score ASC').
with_trigram(trigrams).
map(&:owner)
with_trigram(trigrams)
end

def _add_fuzzy_scopes
Expand Down
11 changes: 10 additions & 1 deletion lib/fuzzily/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ def fuzzily_searchable(*fields)
def _find_by_fuzzy(_o, pattern, options={})
options[:limit] ||= 10

_o.trigram_class_name.constantize.
trigrams = _o.trigram_class_name.constantize.
limit(options[:limit]).
for_model(self.name).
for_field(_o.field.to_s).
matches_for(pattern)
records = _load_for_ids(trigrams.map(&:owner_id))
# order records as per trigram query (no portable way to do this in SQL)
trigrams.map { |t| records[t.owner_id] }
end

def _load_for_ids(ids)
{}.tap do |result|
find(ids).each { |_r| result[_r.id] = _r }
end
end

def _bulk_update_fuzzy(_o)
Expand Down
14 changes: 7 additions & 7 deletions spec/fuzzily/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ def model.name ; 'MyModel' ; end
end

it 'finds matches' do
model.matches_for('Paris').should == [@paris]
model.matches_for('Paris').map(&:owner).should == [@paris]
end

it 'finds close matches' do
model.matches_for('Piriss').should == [@paris]
model.matches_for('Piriss').map(&:owner).should == [@paris]
end

it 'does not confuse fields' do
model.for_field(:name).matches_for('Paris').should == [@paris]
model.for_field(:data).matches_for('Paris').should be_empty
model.for_field(:name).matches_for('Paris').map(&:owner).should == [@paris]
model.for_field(:data).matches_for('Paris').map(&:owner).should be_empty
end

it 'does not confuse owner types' do
model.for_model(Stuff).matches_for('Paris').should == [@paris]
model.for_model(Object).matches_for('Paris').should be_empty
model.for_model(Stuff).matches_for('Paris').map(&:owner).should == [@paris]
model.for_model(Object).matches_for('Paris').map(&:owner).should be_empty
end

context '(with more than one entry)' do
Expand All @@ -69,7 +69,7 @@ def model.name ; 'MyModel' ; end
end

it 'returns ordered results' do
model.matches_for('Palmyre').should == [@palma, @paris]
model.matches_for('Palmyre').map(&:owner).should == [@palma, @paris]
end
end
end
Expand Down

0 comments on commit 4bc7dce

Please sign in to comment.