Skip to content

Instantly share code, notes, and snippets.

@charly
Last active January 16, 2018 17:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charly/6c2311abdcfd386d35f5381fe3b74350 to your computer and use it in GitHub Desktop.
Save charly/6c2311abdcfd386d35f5381fe3b74350 to your computer and use it in GitHub Desktop.
How to join associated models of a PgSearch Document to apply dynamic filter
class PgSearch::Document
belongs_to :article, -> {where("searchable_type = 'Article'")},
foreign_key: 'searchable_id'
belongs_to :info, -> {where("searchable_type = 'Info'")},
foreign_key: 'searchable_id'
end
@searched = PgSearch.multisearch(@query).
with_pg_search_highlight.
includes(:searchable).
left_outer_joins(:info, :article).
where("infos.lang = :lang OR articles.lang= :lang", lang: I18n.locale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment