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

Trying to eager load searchable #53

Closed
alexandru-calinoiu opened this issue Aug 15, 2012 · 5 comments
Closed

Trying to eager load searchable #53

alexandru-calinoiu opened this issue Aug 15, 2012 · 5 comments

Comments

@alexandru-calinoiu
Copy link

Hi fellows,

I am experimenting with multi search, and I am trying to eager load searchable from the document search results.

My approach to open up the Document class and add a scope

module PgSearch
  class Document < ActiveRecord::Base
    scope :by_type, lambda { |type| joins("JOIN #{type.table_name} ON #{type.table_name}.id = #{Document.table_name}.searchable_id AND #{Document.table_name}.searchable_type = '#{type.to_s}'") }
  end
end

This is of course to bypass the polymorphic limitation for eager loading. Now I can write something like:

PgSearch.multisearch("cool").by_type(Dispatch)

Do you think this is a desirable addition to this gem, shall I make a pull request?

Regards,

@Peeja
Copy link
Contributor

Peeja commented Aug 17, 2012

I'm not sure I understand your use case. Why is it useful to eager load Dispatches, but not other searchables?

@alexandru-calinoiu
Copy link
Author

I want to eager load searchables, but they are a polymorphic association, that can't be eager loaded with a simple includes(:searchables). My work around suggests I give a type to searchable so it can be loaded.

@nertzy
Copy link
Collaborator

nertzy commented Sep 9, 2012

According to the latest Rails documentation, eager loading is supported for polymorphic associations. I don't remember this feature being added to Rails, but if it works, that would be great!

Search for "Eager loading is supported with polymorphic associations." on this page:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Eager+loading+of+associations

So, does this work?

PgSearch.multisearch("foo").includes(:searchable)

@nertzy
Copy link
Collaborator

nertzy commented Sep 9, 2012

Also, to clarify, PgSearch.multisearch returns a scope of PgSearch::Document, so you can chain onto it anything that you could call on PgSearch::Document directly.

@nertzy
Copy link
Collaborator

nertzy commented Sep 9, 2012

I just tested it out, and it works! I'm going to close this for now. Please let me know if you run into any problems with it.

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

3 participants