How do I sort my columns inside my AJAX search result?

I have just converted into sunspot but am having problems geting my
sorting of columns to work in the controller. My old controller action
looked like this:

def index
@user = current_user
@products = @user.products.search(params[:search]).order(sort_column +
’ ’ + sort_direction)
end

However I haven’t been able to apply this into my new controller action.

Any ideas?

My complete application:

models/product.rb:

class Product < ActiveRecord::Base
attr_accessible :ean, :name, :price, :stock, :user_id, :sku
belongs_to :user

validates :user_id, presence: true

searchable do
text :ean, :sku, :name
end
end

controllers/products_controller.rb:

class ProductsController < ApplicationController

helper_method :sort_column, :sort_direction

def index
@user = current_user
@search = @user.products.search do
fulltext params[:search]
end
@products = @search.results
end

private

def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] :
“name”
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] :
“asc”
end
end

views/products/index.html.erb:

<%= form_tag products_path, :method => :get, :id => “products_search” do
%>

<%= text_field_tag :search, params[:search] %> <%= submit_tag "Search", :name => nil, class: "btn btn-large btn-primary" %>

<%= render 'products' %>
<% end %>

views/index.js.erb:

$(’#products’).html(’<%= escape_javascript(render(“products”)) %>’);

views/products/_products.html.erb:

<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>

<% for product in @products %> <% end %>
<%= sortable "sku", "SKU" %> <%= sortable "name", "Name" %> <%= sortable "stock", "Stock" %> <%= sortable "price", "Price" %> <%= sortable "ean", "EAN" %>
<%= product.sku %> <%= product.name %> <%= product.stock %> <%= product.price %> <%= product.ean %>

assets/javascripts/application.js:

$(function () {
$(’#products th a’).live(‘click’,
function () {
$.getScript(this.href);
return false;
}
);

$(’#products_search input’).keyup(function () {
$.get($("#products_search").attr(“action”),
$("#products_search").serialize(), null, ‘script’);
return false;
});
});

if @search is not an ActiveRecord::Relation object you cant chain it
with
another ActiveRecord::Relation object. What class does the search
function
returns? are you using a gem? the search function could be returning an
array in which case you have to build your own sorting mechanism.

Radhames Brito wrote in post #1065828:

if @search is not an ActiveRecord::Relation object you cant chain it
with
another ActiveRecord::Relation object. What class does the search
function
returns? are you using a gem? the search function could be returning an
array in which case you have to build your own sorting mechanism.

Hi rbritom.

I am using the sunspot_rails gem (GitHub - sunspot/sunspot: Solr-powered search for Ruby objects).

My old search function (and sorting function) was build after Railscast
240 (#240 Search, Sort, Paginate with AJAX - RailsCasts),
but as I needed to search through multiple columns I implemented Sunspot
instead. Not quite sure if @search is an ActiveRecord::Relation. Fairly
new to Rails.

Anders A. wrote in post #1065829:

Radhames Brito wrote in post #1065828:

if @search is not an ActiveRecord::Relation object you cant chain it
with
another ActiveRecord::Relation object. What class does the search
function
returns? are you using a gem? the search function could be returning an
array in which case you have to build your own sorting mechanism.

Hi rbritom.

I am using the sunspot_rails gem (GitHub - sunspot/sunspot: Solr-powered search for Ruby objects).

My old search function (and sorting function) was build after Railscast
240 (#240 Search, Sort, Paginate with AJAX - RailsCasts),
but as I needed to search through multiple columns I implemented Sunspot
instead. Not quite sure if @search is an ActiveRecord::Relation. Fairly
new to Rails.

When I look around on the internet I can see that people uses sort_by to
make it work, e.g.:

def index
@user = current_user
@search = @user.products.search do
fulltext params[:search]
order_by sort_column, sort_direction
end
@products = @search.results
end

However it stil breaks. With this error message:

Sunspot::UnrecognizedFieldError in ProductsController#index
No field configured for Product with name ‘name’