Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to search in has_many association using Sunspot/Solr?
    text
    copied!<p>Here is what my initial searchable block looks like in my User model:</p> <pre><code>searchable do text :name integer :sport_ids, multiple: true do sports.map(&amp;:id) end integer :position_ids, multiple: true do positions.map(&amp;:id) end integer :city_id integer :school_id string :state end </code></pre> <p>How do I search by has_many associations? I need it to return each Athlete who has a specified ID in their sports or sport_positions. So if someone selects "Basketball" from a dropdown, ID of 2 is passed to my search method and it needs to return Athletes who have sport_id of 2 in their collection of sport_ids. Here is how sports and sport_positions are declared in the User model:</p> <p><code>has_and_belongs_to_many :positions, :class_name =&gt; "SportPosition", :join_table =&gt; "user_sport_positions", :uniq =&gt; true</code></p> <p><code>has_many :sports, :through =&gt; :user_sports, order: "user_sports.created_at", class_name: "Sport"</code></p> <p><code>has_many :user_sports</code></p> <p><strong>:::EDIT:::</strong></p> <p>This worked for a minute after I reindexed, then all of a sudden I started getting this error:</p> <pre><code>Sunspot::UnrecognizedFieldError (No field configured for Athlete with name 'sport_ids'): app/models/search.rb:12:in `block in execute' </code></pre> <p>here is my Search model:</p> <pre><code>class Search &lt; ActiveRecord::Base attr_accessible :coach_id, :sport_id, :gpa_min, :gpa_max, :sport_position_id, :classification, :city_id, :state, :school_id, :athlete_name belongs_to :coach def self.execute(params, page = nil) Sunspot.search(Athlete) do |query| query.with :public, true query.with :removed_from_listing, false query.fulltext params[:athlete_name] unless params[:athlete_name].blank? query.with :sport_ids, params[:sport_id] unless params[:sport_id].blank? query.with :position_ids, params[:sport_position_id] unless params[:sport_position_id].blank? query.with(:state).equal_to(params[:state]) unless params[:state].blank? query.with(:classification).equal_to(params[:classification]) unless params[:classification].blank? query.with :city_id, params[:city_id] unless params[:city_id].blank? query.with :school_id, params[:school_id] unless params[:school_id].blank? query.with(:current_gpa).between(params[:gpa_min]..params[:gpa_max]) unless params[:gpa_min].eql?("0.0") &amp;&amp; params[:gpa_max].eql?("5.0") query.paginate page: page unless page.blank? end end end </code></pre> <p><strong>NOTE:</strong> To make this even more strange, I have a field called "recruit_year" that is an integer attribute. I was getting the same error on this field saying "No field configured" blah blah. That error usually only happens on <code>text</code> fields if you try to do a comparison like equal_to or treat it like a <code>string</code>.</p> <p>Help?</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