Skip to content

Instantly share code, notes, and snippets.

@bhoggard
Created October 4, 2012 23:11
Show Gist options
  • Save bhoggard/3837054 to your computer and use it in GitHub Desktop.
Save bhoggard/3837054 to your computer and use it in GitHub Desktop.
Working Mongoid/Sunspot search including without() against a relation
require 'spec_helper'
class MyObject
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Sunspot::Rails::Searchable
field :title, type: String
validates_presence_of :title
attr_accessible :title
has_and_belongs_to_many :rooms, index: true
searchable do
text :title
string :room_ids, :multiple => true
end
end
class Room
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Sunspot::Rails::Searchable
has_and_belongs_to_many :products, index: true
end
describe MyObject do
it "should be able filter by without() against an empty array", :solr => true do
title = 'Cool Thing'
obj = MyObject.create!(title: title)
room = Room.create!
obj.index!
search = MyObject.solr_search do
without(:room_ids, [room.id])
fulltext title
end
search.results.count.should == 1
search = MyObject.solr_search do
without(:room_ids, room.id)
fulltext title
end
search.results.count.should == 1
obj.rooms << room
obj.save!
obj.index!
search = MyObject.solr_search do
without(:room_ids, room.id)
fulltext title
end
search.results.count.should == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment