Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The method you've found for searching multiple models is correct. However, it appears that the meaning of your search is not what you intended. It looks as if you're trying to say:</p> <blockquote> <p>Give me all <code>Profile</code> records with these <code>country</code> and <code>state</code> values, <strong>and</strong> whose <code>Match</code> record has this <code>looking_for_education</code> value</p> </blockquote> <p>Your search, however, says:</p> <blockquote> <p>Give me all records of type <code>Profile</code> <strong>or</strong> <code>Match</code> that have <strong>all</strong> of these <code>country</code>, <code>state</code> and <code>looking_for_education</code> values</p> </blockquote> <p>Because neither <code>Profile</code> nor <code>Match</code> have all of these fields in their respective <code>searchable</code> blocks, no single record can match the conditions you specify.</p> <p>If I'm correct about your intended behaviour above, then you need to include the profile's associated match information in the profile's <code>searchable</code> block, like so:</p> <pre><code>class Profile &lt; ActiveRecord::Base has_one :match searchable do string(:country) string(:state) string(:city) string(:looking_for_education) { match.looking_for_education } integer(:age_from) { match.age_from } integer(:age_to) { match.age_to } end end </code></pre> <p>Here, we've told Sunspot to index properties of the profile's match association as if they lived on the profile itself. In the respective blocks, we've told Sunspot how to populate these values when the profile is indexed.</p> <p>This will allow you to write your search using only the <code>Profile</code> model:</p> <pre><code>def index @search = Sunspot.search Profile do with(:country, params[:country]) with(:state, params[:state]) with(:looking_for_education, params[:looking_for_education]) with(:age).between(params[:age_from]..params[:age_to]) end @profiles = @search.results end </code></pre> <p>This search will return only <code>Profile</code> records, while still reflecting the properties of each profile's match association, because we stored them when the profile was indexed.</p> <p>Note that this increases complexity when you index your models. If a <code>Match</code> record changes, its associated profile now needs to be reindexed to reflect those changes.</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