Skip to content

Commit

Permalink
Support NULL values when searching multiple columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Hutchins & Peter Jaros committed Nov 10, 2010
1 parent fd34c1c commit cfadef7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pg_search_scope(name, matches)
options = options_proc.call(*args)

matches_concatenated = Array.wrap(options[:matches]).map do |match|
"#{quoted_table_name}.#{connection.quote_column_name(match)}"
"coalesce(#{quoted_table_name}.#{connection.quote_column_name(match)}, '')"
end.join(" || ' ' || ")

conditions = "to_tsvector('simple', #{matches_concatenated}) @@ plainto_tsquery('simple', :query)"
Expand Down
12 changes: 12 additions & 0 deletions spec/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@
end
end

it "builds a scope for searching on multiple columns where one is NULL" do
model_with_pg_search.class_eval do
pg_search_scope :search_text_and_content, [:title, :content]
end

included = model_with_pg_search.create!(:title => 'foo', :content => nil)

results = model_with_pg_search.search_text_and_content('foo')

results.should == [included]
end

it "builds a dynamic scope when passed a lambda" do
model_with_pg_search.class_eval do
pg_search_scope :search_title_or_content, lambda { |query, pick_content|
Expand Down

0 comments on commit cfadef7

Please sign in to comment.