Model.excludes(state: "this).excludes(state: "that")
produces:
"state"=>{"$ne"=>"that"}
but what I really want is to exclude all records that have either "this" or "that"
Model.excludes(state: "this).excludes(state: "that")
produces:
"state"=>{"$ne"=>"that"}
but what I really want is to exclude all records that have either "this" or "that"
Here's a working test that uses Model.not_in
http://two.mongoid.org/docs/querying/criteria.html#not_in
app/models/model.rb
class Model
include Mongoid::Document
field :state, type: String
end
test/unit/model_test.rb:
require 'test_helper'
class ModelTest < ActiveSupport::TestCase
def setup
Model.delete_all
end
test "not_in" do
Model.create({ state: 'this' })
Model.create({ state: 'that' })
Model.create({ state: 'not this or that'})
assert_equal(3, Model.count)
p Model.not_in(state: ['this', 'that']).to_a
assert_equal(1, Model.not_in(state: ['this', 'that']).to_a.count)
end
end
rake test:
Run options:
# Running tests:
[#<Model _id: 50e6fdef29daeb27e5000003, _type: nil, state: "not this or that">]
.
Finished tests in 0.021175s, 47.2255 tests/s, 94.4510 assertions/s.
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips