Ruby on rails Mongoid排除状态为“的所有记录”;这";或;那是什么?
Ruby on rails Mongoid排除状态为“的所有记录”;这";或;那是什么?,ruby-on-rails,ruby-on-rails-3,mongodb,mongoid,Ruby On Rails,Ruby On Rails 3,Mongodb,Mongoid,Model.excludes(状态:“this”).excludes(状态:“that”) 产生: “state”=>{“$ne”=>“that”} 但我真正想要的是排除所有包含“this”或“that”的记录这里有一个使用Model.not_的工作测试 app/models/model.rb class Model include Mongoid::Document field :state, type: String end test/unit/model_test.rb: re
Model.excludes(状态:“this”).excludes(状态:“that”)
产生:
“state”=>{“$ne”=>“that”}
但我真正想要的是排除所有包含“this”或“that”的记录这里有一个使用Model.not_的工作测试 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
需要“测试助手”
类ModelTest
耙试验:
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
运行选项:
#运行测试:
[#]
.
以0.021175秒、47.2255次测试/秒、94.4510次断言/秒的速度完成测试。
1次测试,2次断言,0次失败,0次错误,0次跳过
可能重复:不太可能,它们排除在两个不同的属性上。我想排除在同一个属性上,只使用两个不同的值。谢谢你的评论,它至少给了我一些其他的东西。你尝试过类似:Model.excludes(state:“this”,state:“that”)的东西吗
?是的。没有骰子。第二个会覆盖第一个。(如果两个属性不同(:state和:something_其他),Mongoid会正确构造查询,但当它是同一个属性时就不会了)。这个应该会有帮助:是的,这是一个赢家。谢谢!(我真的很感谢你的工作,包括测试!!我希望更多的答案是这样的。太棒了。)谢谢你的赞赏。不客气,我希望你喜欢使用MongoDB。