Rails Searchkick / Elasticsearch has_many and belongs_to associations

后端 未结 1 1697
春和景丽
春和景丽 2021-02-10 17:39

Im trying to use Searchkick to run a search and return based on multiple models.

My book model contains this

class Book < ActiveRecord::Base

  sear         


        
1条回答
  •  长发绾君心
    2021-02-10 18:00

    In your Book model you need to have a search_data block for the indexing.

    def search_data
      attributes.merge(
        author_name: author(&:name)
        publisher_name: publisher(&:name)
        subjects_name: subjects.map(&:name)
      )
    end
    

    this will add the associations to your index.

    You use the .map method for the has_many associations.

    0 讨论(0)
提交回复
热议问题