Model.excludes(state: "this).excludes(state: "that")
产生:
"state"=>{"$ne"=>"that"}
但我真正想要的是排除所有具有“this”或“that”的记录
Model.excludes(state: "this).excludes(state: "that")
产生:
"state"=>{"$ne"=>"that"}
但我真正想要的是排除所有具有“this”或“that”的记录
这是一个使用 Model.not_in 的工作测试
http://two.mongoid.org/docs/querying/criteria.html#not_in
应用程序/模型/model.rb
class Model
include Mongoid::Document
field :state, type: String
end
测试/单元/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
耙式测试:
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