mondayguy
mondayguy

Reputation: 991

Sunspot solr "No field configured for User with name 'C2'"

I want to add ordering on my sunspot search. Without ordering search works perfectly. Here is my search method:

def search
    @search = User.search do
      keywords params[:q]
      paginate page: params[:page]
      order_by(:C2, :desc)
    end
    @users = @search.results
    respond_to do |format|
      format.html { render :action => "index" }
    end
  end

My User model

class User < ActiveRecord::Base
  searchable do
    text :C1
    text :C2
  end
end

So I'm getting

No field configured for User with name 'C2'

on the order_by line

Also I've thought that MAYBE cause of the problem is that C2 is text, not string, so I've done next hack:

class User < ActiveRecord::Base
  searchable do
    text :C1
    text :C2
    string  :sort_title do
      C2.downcase
    end
  end
end

In order to add order_by (:sort_title, :desc) in the controller, but for some reason when I reindex this I get:

NameError: uninitialized constant User::C2

What am I doing wrong?

Upvotes: 0

Views: 718

Answers (1)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

Try this:

searchable do
  text :C1
  text :C2
  string :sort_title do |user|
    user.C2.downcase
  end
end

Then try to reindex, and it should just go well..

Upvotes: 1

Related Questions