I don't get how I can make a search using sunspot to work on a has_many association. The user selects skills from a select_tag field and the results return records that match the skill_ids.
Just for the record, when I press the search button I get this back:
SOLR Request (294.1ms) [ path=select parameters={fq: ["type:User"], q: ["4"], fl: "* score", qf: "", defType: "edismax", start: 0, rows: 30} ]
The q: ["4"]
mentions the number 4 which is the ID of the skill I searched for. I get no results when I was suppose to get something back. I do get all users back as results when I submit with no parameters.
What I have done and figured out so far can be found below. Any tips or help to the right direction is greatly appreciated.
(Please let me know if you need any other info and I will add it right away)
My structure is as follows:
In Users model (user.rb)
has_many :user_skills, dependent: :destroy
has_many :skills, through: :user_skills
searchable do
integer :skill_ids, :multiple => true do
skills.map(&:id)
end
end
In Skills model (skill.rb)
belongs_to :user
has_many :user_skills
has_many :users, through: :user_skills
searchable do
integer :id, :multiple => true
end
DB schema:
t.string "name"
t.string "category"
t.string "slug"
In User_skills model (user_skill.rb)
belongs_to :user
belongs_to :skill
DB schema:
t.integer "user_id"
t.integer "skill_id"
In my search controller I have the following code segment:
@search = Sunspot.search(User) do
fulltext params[:s_skills]
end
@users = @search.results
params[:s_skills] are the skill_ids extracted from a select_tag:
<%= select_tag :s_skills, options_for_select(Skill.all.collect{|e| [e.name,e.id]}, skills), {:id => 'e2', :multiple => true, :placeholder => "Input skills", class: "input-search2" } %>
which takes skills's name and returns their ids.
You are not actually searching in the skill_ids
field you have indexed.
You must add something like:
User.search do
fulltext params[:free_text] #<-- YOU DON'T NEED THIS, PROBABLY?
with(:skill_ids, params[:s_skills]) if params[:s_skills]
end