Question

I'm having trouble running pg_search. I think it's set up correctly, but for some reason when trying to run a search, this comes up:

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

Course model:

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

Search Controller:

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

In the html:

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

Is it possible that this is caused by the .each? If I only keep <%= @course %>, the page loads up and shows:

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

Am I supposed to convert the resulting array before passing the .each function?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top