Michael Giovanni Pumo
Michael Giovanni Pumo

Reputation: 14774

Rails with Sunspot - How do I output the facet results?

I'm extremely new to Ruby, Rails and Sunspot, let alone Solr! So, excuse me if this all seems a bit 'dumb'.

I have the following setup.

Model

class Pin < ActiveRecord::Base

    belongs_to :user
    has_many :replies
    has_one :type

    validates :user_id, presence: true
    validates :type_id, presence: true
    validates :title, presence: true
    validates :description, presence: true

    searchable do

        text :title, :boost => 5
        text :description
        integer :type_id

    end

end

Controller

def index

@search = Pin.search do

    fulltext params[:keywords]
    facet :type_id

end

@pins = @search.results

end

View

<% @search.facet(:type_id).rows.each do |facet| %>
            <%= puts "Type #{facet.value} has #{facet.count} pins!" %>
        <% end %>

But when I try to view this on the page, all I get is:

[<Sunspot::Search::FacetRow:1 (6)>, <Sunspot::Search::FacetRow:2 (6)>]

Am I missing something here?

I want to be able to output the available types for each pin.

Thanks! Mikey

UPDATE:

So, it turns out I should be doing this in my view:

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

This now outputs the type ID from the pin. Great! However, if this ID is joined to another table, where the type TITLE is also held, how can I grab that with the facet??

So, all pins have a type id...that is related to a types table. My models are set up correctly but I could not work out how to do this.

Thanks!

Upvotes: 0

Views: 272

Answers (1)

Ali H Dinani
Ali H Dinani

Reputation: 31

This is actually an ERB issue

When you do:

<%= @search.facet(:type_id).rows.each do |facet| %>

You are outputting the result of

@search.facet(:type_id).rows.each do |facet|

Replace it with this instead

<% @search.facet(:type_id).rows.each do |facet| %>

(Notice the lack of '=')

Upvotes: 1

Related Questions