mercredi 14 octobre 2015

Sunspot Facets with has_many associations

I'm new to Sunspot so please have pity! I'm trying to add a facet for search on my profiles, the attribute I'd like to make a facet is company name which belongs to profiles thru a has_many association but I'm stuck on how to do this.

Here's a snippet of my Profile model:

class Profile < ActiveRecord::Base

belongs_to :user

attr_accessible :user, :interests_summary, :linked_url, :personal_summary, :phone, :profile_picture, :delete_profile_picture, :user_id, :website_url, :workexperiences_attributes

has_many :workexperiences, :dependent => :destroy
  accepts_nested_attributes_for :workexperiences, :reject_if => lambda { |a| a[:company_name].blank? }, :allow_destroy => true

  searchable do
    text :personal_summary

    text :workexperiences do
      workexperiences.map(&:company_name)
    end
  end 

Here's my Profile Controller:

  def index
    @search = Profile.search do
      fulltext params[:search]

      facet :company_name

      paginate(:page => params[:page] || 1, :per_page => 24)
    end
    @profiles = @search.results

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @profiles }
    end
  end

In my index, I have

<% @search.facet(:company_name).rows.each do |row| %>
    <%= row.value %>
<% end %>

However rails throws

Sunspot::UnrecognizedFieldError in ProfilesController#index
No field configured for Profile with name 'company_name'

Question is how do I add a facet for company name in my index?

Aucun commentaire:

Enregistrer un commentaire