0

我在运行 pg_search 时遇到问题。我认为它设置正确,但由于某种原因,在尝试运行搜索时,会出现以下情况:

SQLite3::SQLException: unrecognized token: ":": SELECT "courses".*, ((ts_rank((to_tsvector('simple',
coalesce("courses"."title"::text, ''))), (to_tsquery('simple', ''' ' || 'finance' || ' ''')), 0))) AS
pg_search_rank FROM "courses"  WHERE (((to_tsvector('simple', coalesce("courses"."title"::text, '')))
@@ (to_tsquery('simple', ''' ' || 'finance' || ' ''')))) ORDER BY pg_search_rank DESC, "courses"."id" ASC

课程模式:

include PgSearch
scope :by_course_title, lambda { |ttl|
  _by_course_title(ttl) if ttl.present? }
pg_search_scope :_by_course_title, against: :title

搜索控制器:

def index
    @course = Course.by_course_title(params[:fname])
end

在 html 中:

<% @course.each do |courses| %>
    <div><%= courses %></div>
<% end %>

这可能是由.each引起的吗?如果我只保留 <%= @course %>,页面会加载并显示:

#<ActiveRecord::Relation:0x....>

我应该在传递 .each 函数之前转换结果数组吗?

4

1 回答 1

4

You have a PostgreSQL specific library (PgSearch) and you're using SQLite3 as your database. Pick one or the other, but not both.

The casting operator :: in coalesce("courses"."title"::text, '') is valid for PostgreSQL only. With SQLite you can leave out casting completely (the ::text part), but as mentioned before, you likely won't get PgSearch to work correctly unless you switch over to a PostgreSQL database.

于 2013-08-08T00:22:01.017 回答