Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search

Home Page:http://www.casebook.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to include/eager_load associations from additional attributes?

jcohenho opened this issue · comments

In the docs, you can eager load the searchable associations via PgSearch.multisearch("Square").includes(:searchable).

After adding additional attributes via migrations and configuration:

   # migration file
   create_table :pg_search_documents do |t|
     t.text :content
     t.references :author, index: true
     t.belongs_to :searchable, polymorphic: true, index: true
     t.timestamps null: false
   end

  # model.rb
  multisearchable(
    against: [:title, :body],
    additional_attributes: -> (article) { { author_id: article.author_id } }
  )

I now have the ability to do:

PgSearch.multisearch(params['search']).where(author_id: 2)

However, I would like to also eager load the Authors association to prevent n+1 queries, but it doesn't seem like this is possible.
I've tried PgSearch.multisearch(params['search']).includes(:searchable, :author).where(author_id: 2) but this gives a missing association error. Any suggestions?

@nertzy any insights here?

Did you add author association to the model?