0

I am currently using Pg gem search in my application. Now, I want to use the pg search to search for the data across two models which are school and teacher. I created a welcome page let the audience use the search functionality there. However, at the moment I am wondering how can I retrieve the result after the audience enter the query, for example, if they type a specific name of a teacher or a school, I want to return that teacher/school with a link so that the user can click on and find out more information about that school/teacher. Here are my files:

search.haml:

.row
  %h1.text-center Tìm Trường Học và Giáo Viên
  = form_tag search_welcome_index_url, :id => 'custom-search-input', method: :get, class: "input-group col-md-12", role: "search" do
    .input-group
      = text_field_tag :search,
            params[:search], class: "search-query form-control", placeholder: "Tên trường học, ví dụ: Đại Học Bách Khoa Hồ Chí Minh...."
      %span.input-group-btn
        = button_tag( :class => "btn btn-danger") do
          .search-size
            %span.glyphicon.glyphicon-search

= @results

welcome_controller.rb:

class WelcomeController < ApplicationController

  def welcome

  end

  def search
    @results = PgSearch.multisearch(params[:search])
  end
end

teacher.rb:

class Teacher < ActiveRecord::Base
  include PgSearch
  multisearchable against:  [:full_name]
  belongs_to :school
  has_many :ratings
end

school.rb:

class School < ActiveRecord::Base
  include PgSearch
  multisearchable :against => [:name]
  has_many :teachers, dependent: :destroy
end

routes.rb:

Rails.application.routes.draw do
  devise_for :users

  resources :welcome do
    collection do
      get 'search'
    end
  end

  resources :schools do
    collection do
      get 'search'
    end
    resources :teachers do
      collection do
        get 'search'
      end
    end
  end

  resources :teachers do
    resources :ratings
  end

  root 'welcome#welcome'
end
  • Are you getting any errors? – sureshprasanna70 Feb 18 '16 at 14:20
  • @sureshprasanna70, thanks for the quick relpy. I dont get any errors though. However, I get this `#` after I entered the search query. I am wondering how I can retrieve the school/teacher's name after I enter the search query. I have been learning rails by myself for 5 months (I wanna create a startup) and this is the hardest problem I have come so far. I have tried elastic search(searchkick), sunspot, solr, and now postgresql full search functionality. I am really stuck now. :( – Final Fantasy VIII Feb 18 '16 at 14:33
  • Can you give the size of the relation?. Try `relation.size` – sureshprasanna70 Feb 18 '16 at 14:35
  • @sureshprasanna70, hi, can you be more specific? Because I got the error `undefined local variable or method 'relation'` – Final Fantasy VIII Feb 18 '16 at 14:41
  • The exact one would be `@results.size`. You should check this after completing the search – sureshprasanna70 Feb 18 '16 at 14:43
  • @sureshprasanna70 ok, I edited the code, and I got 1. Does it mean it found one result that matches with my search query? I tried to use PgSearch.multisearch in rails console and it works fine, I can find the exact the school name or teacher name. However, I just dont know how to show it on my search page. – Final Fantasy VIII Feb 18 '16 at 14:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103858/discussion-between-sureshprasanna70-and-final-fantasy-viii). – sureshprasanna70 Feb 18 '16 at 14:46

0 Answers0