mongoid/mongoid — Issue #1626: all_in/any_in criteria on an embedded document's array field
Author: joshuabowers
Created: 2012-01-22 13:35:26+00:00
State: open

Given a set of class definitions and sample data like the following:

```ruby

class Foo
include Mongoid::Document
field :widgets, type: Array, default: []
embeds_many :bars
end

class Bar
include Mongoid::Document
field :widgets, type: Array, default: []
embedded_in :foo
end

Foo.create(widgets: ["foo", "bar", "baz"])
Foo.create(widgets: ["bar", "baz", "qux"])
Foo.create(widgets: ["baz", "qux", "png"])

foo = Foo.first
foo.bars.create(widgets: ["foo", "bar", "baz"])
foo.bars.create(widgets: ["bar", "baz", "qux"])
foo.bars.create(widgets: ["baz", "qux", "png"])
```

I get wonky results when performing either an "all_in" or an "any_in" when matching widgets against an array of regexs: notably, matching the widgets coming from Foo works in a way that seems sensible, while matching widgets coming from Bar does not. For example:

```ruby
Foo.all_in(widgets: [/foo/i]).count == 1
Foo.all_in(widgets: [/bar/i]).count == 2
Foo.all_in(widgets: [/baz/i]).count == 3

foo.bars.all_in(widgets: [/foo/i]).count == 1 # returns false; count == 0
foo.bars.all_in(widgets: [/bar/i]).count == 2 # returns false; count == 0
foo.bars.all_in(widgets: [/baz/i]).count == 3 # returns false; count == 0
```

To clarify: all_in seems to allow me to find all instances of a (top-level) document which have an array field whose entries match a set of regexs. (This seems like the correct behavior to me.) However, performing the same type of search on an embedded document does not seem to behave in the same fashion.

Am I missing something about all_in/any_in? Am I misusing or abusing them in some nefarious way?

Tested in version 2.4.1 in ruby 1.9.2-p290.