Created
October 4, 2012 23:11
-
-
Save bhoggard/3837054 to your computer and use it in GitHub Desktop.
Working Mongoid/Sunspot search including without() against a relation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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