Permalink
Browse files

Use table name when referring to tsvector column.

Closes #28.
  • Loading branch information...
1 parent 842aff0 commit 56e540660e5f59c7e0f56a6bd277c737614a8d27 Peter Jaros & Grant Hutchins committed May 27, 2012
Showing with 29 additions and 9 deletions.
  1. +2 −1 lib/pg_search/features/tsearch.rb
  2. +27 −8 spec/pg_search_spec.rb
@@ -63,7 +63,8 @@ def tsquery
def tsdocument
if @options[:tsvector_column]
- @options[:tsvector_column].to_s
+ column_name = connection.quote_column_name(@options[:tsvector_column])
+ "#{quoted_table_name}.#{column_name}"
else
@columns.map do |search_column|
tsvector = "to_tsvector(:dictionary, #{@normalizer.add_normalization(search_column.to_sql)})"
View
@@ -610,7 +610,7 @@
end
context "using a tsvector column" do
- with_model :ModelWithPgSearchUsingTsVectorColumn do
+ with_model :ModelWithTsvector do
table do |t|
t.text 'content'
t.tsvector 'content_tsvector'
@@ -619,16 +619,16 @@
model { include PgSearch }
end
- let!(:expected) { ModelWithPgSearchUsingTsVectorColumn.create!(:content => 'tiling is grouty') }
- let!(:unexpected) { ModelWithPgSearchUsingTsVectorColumn.create!(:content => 'longcat is looooooooong') }
+ let!(:expected) { ModelWithTsvector.create!(:content => 'tiling is grouty') }
+ let!(:unexpected) { ModelWithTsvector.create!(:content => 'longcat is looooooooong') }
before do
ActiveRecord::Base.connection.execute <<-SQL
- UPDATE #{ModelWithPgSearchUsingTsVectorColumn.table_name}
- SET content_tsvector = to_tsvector('english'::regconfig, "#{ModelWithPgSearchUsingTsVectorColumn.table_name}"."content")
+ UPDATE #{ModelWithTsvector.table_name}
+ SET content_tsvector = to_tsvector('english'::regconfig, "#{ModelWithTsvector.table_name}"."content")
SQL
- ModelWithPgSearchUsingTsVectorColumn.pg_search_scope :search_by_content_with_tsvector,
+ ModelWithTsvector.pg_search_scope :search_by_content_with_tsvector,
:against => :content,
:using => {
:tsearch => {
@@ -639,11 +639,30 @@
end
it "should not use to_tsvector in the query" do
- ModelWithPgSearchUsingTsVectorColumn.search_by_content_with_tsvector("tiles").to_sql.should_not =~ /to_tsvector/
+ ModelWithTsvector.search_by_content_with_tsvector("tiles").to_sql.should_not =~ /to_tsvector/
end
it "should find the expected result" do
- ModelWithPgSearchUsingTsVectorColumn.search_by_content_with_tsvector("tiles").map(&:id).should == [expected.id]
+ ModelWithTsvector.search_by_content_with_tsvector("tiles").map(&:id).should == [expected.id]
+ end
+
+ context "when joining to a table with a column of the same name" do
+ with_model :AnotherModel do
+ table do |t|
+ t.string :content_tsvector # the type of the column doesn't matter
+ t.belongs_to :model_with_tsvector
+ end
+ end
+
+ before do
+ ModelWithTsvector.has_many :another_models
+ end
+
+ it "should refer to the tsvector column in the query unambiguously" do
+ expect {
+ ModelWithTsvector.joins(:another_models).search_by_content_with_tsvector("test").all
+ }.not_to raise_exception
+ end
end
end

0 comments on commit 56e5406

Please sign in to comment.