I have a following model:
class Article < ActiveRecord::Base
define_index do
indexes content
indexes :name, sortable: true
has type
end
end
and special type of an article is:
class About < Article
end
and the same for Contact
I would like to have searchable articles index action without articles with type of “About” or “Contact”
class ArticlesController < ApplicationController
load_and_authorize_resource
def index
@articles = Article.search(params[:search],
:with_all => {:type => nil},
:page => params[:page],
:per_page => 10)
end
end
But the @articles instance variable contains everytime also “About” & “Contact” articles.
This is very strange (seems like will_paginate messing everything up):
@articles = Article.search(
:without => {:type => %w(About Contact)}).include?(About.first) # false
@articles = Article.search(
:without => {:type => %w(About Contact)},
:page => 1,
:per_page => 1000).include?(About.first) # true
=============================================================================
Finally I did:
class Article < ActiveRecord::Base
define_index do
indexes content
indexes :name, sortable: true
has "CRC32(type)", :as => :article_type, :type => :integer
end
end
and:
class ArticlesController < ApplicationController
load_and_authorize_resource
def index
@articles = Article.search(params[:search],
:without => {:article_type => ["About".to_crc32, "Contact".to_crc32]},
:page => params[:page],
:per_page => 10)
end
end
and it works. Thanks guys!
From the Thinking Sphinx FAQ: