Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to include/eager_load associations from additional attributes? #470

Open
jcohenho opened this issue Jul 7, 2021 · 2 comments
Open

Comments

@jcohenho
Copy link

jcohenho commented Jul 7, 2021

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?

@jcohenho
Copy link
Author

@nertzy any insights here?

@AlexChuv
Copy link

Did you add author association to the model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants