Note that there are some explanatory texts on larger screens.

plurals
  1. POTire best practices with json/many nested active models
    text
    copied!<p>I have a pretty complicated model hierarchy that I want to search. Basically I have a person, who has a resume and that has a lot of things and I want to return the person who matches my free text search ("army" brings up the people were in the army, searching for "super awesome division" returns the people who are in that division etc.</p> <p>This is an example of what I have so far (not complete)</p> <pre><code>class Person &lt; ActiveRecord::Base has_many :certifications, :as =&gt; :certificationable, :dependent =&gt; :destroy has_many :educations, :as =&gt; :educationable, :dependent =&gt; :destroy has_many :phones, :as =&gt; :phoneable, :dependent =&gt; :destroy has_many :emails, :as =&gt; :emailable, :dependent =&gt; :destroy has_one :resume, :dependent =&gt; :destroy belongs_to :division belongs_to :employment_status belongs_to :user mapping do indexes :id, index: :not_analyzed indexes :first, analyzer: 'keyword' indexes :middle, analyzer: 'keyword' indexes :last, analyzer: 'keyword' indexes :sector indexes :job_title indexes :div_title indexes :loc indexes :department indexes :unit indexes :unit_name, analyzer: 'snowball' indexes :division, :analyzer =&gt; 'snowball', :class =&gt; [Division], :type =&gt; :nested indexes :employment_status indexes :emails do indexes :address end indexes :phones do indexes :number end indexes :resume do indexes :military_services do indexes :military_rank do indexes :title indexes :abbreviation end end end end def to_indexed_json to_json( except: [:avatar], include: { division: { only: [:name, :abbrev, :id] }, emails: { only: [:address] }, phones: { only: [:number] }, resume: { include: { military_services: { exclude: [:updated_at, :created_at], include: { military_rank: { only: [:title, :abbreviation] } } } } } } ) end def self.search(params, options = {}) tire.search(options) do query do boolean do must { string params[:query] } if params[:query].present? must { term :division_id, params[:division_id] } if params[:division_id].present? end end sort { by :first, 'asc' } facet :division do terms :division_id end end end </code></pre> <p>end</p> <p>This seems to work but it's kind of unwieldly because I have to reach pretty deep into some of these models for both the indexing and the json. Is this the best way to accomplish my goal? I keep feeling like there must be a way to index and map all of my models and their relations and then relate them back to the Person.</p> <p>I'm also struggling with the json as it's pretty unwieldly. Do people use jbuilder or active_model_serializers for this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload