0

Model.excludes(state: "this).excludes(state: "that")

生成:

"state"=>{"$ne"=>"that"}

しかし、私が本当に欲しいのは、「これ」または「あれ」のいずれかを持つすべてのレコードを除外することです

4

1 に答える 1

1

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
于 2013-01-04T16:10:52.703 に答える