Finding posts under a category name keyword with pg_search on Rails 5
Finding posts under a category name keyword with pg_search on Rails 5
I'm using gem pg_search
and doing multi-search for Website and Post tables. I am able to search by records title, but I also want to be able to search records based on categories they belong to.
pg_search
So, for example, when I type "typographic" (category name) to search input, all Website and Post records under that category should be listed, as well as all records that have "Typographic" in the title.
I think it can be done with association but I could not make it work 🤷♂️
_header.html.erb
<%= form_tag search_index_path, method: :get do %>
<%= text_field_tag :query, params[:query], placeholder: "Search" %>
<% end %>
search_controller.rb
class SearchController < ApplicationController
def index
@pg_search_result = PgSearch.multisearch(params[:query])
end
end
models/post.rb
class Post < ApplicationRecord
include PgSearch
multisearchable :against => :title,
using: {tsearch: {disctionary: "english"}},
associated_against: {category: :title},
ignoring: :accents
models/website.rb
class Website < ApplicationRecord
include PgSearch
multisearchable :against => [:title, :website_url],
using: {tsearch: {disctionary: "english"}},
associated_against: {website_category: :title},
ignoring: :accents
Website
will this help: github.com/Casecommons/pg_search#pg_search_scope?
– Jagdeep Singh
Jun 29 at 8:04
@JagdeepSingh I just udpated the question. I basically do multip search for Post and Website tables
– Designer
Jun 29 at 8:44
@JagdeepSingh I tried that associated_against: { categories: :title } from the link, but it doesnt return records for that :/
– Designer
Jun 29 at 8:45
@Designer can you try to rebuild the indexes again and search them
– rohit
Jun 29 at 9:31
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why the
Website
model in question?– Jagdeep Singh
Jun 29 at 7:55