File tree Expand file tree Collapse file tree 3 files changed +378
-126
lines changed
lib/mongoid/association/embedded/embeds_many Expand file tree Collapse file tree 3 files changed +378
-126
lines changed Original file line number Diff line number Diff line change @@ -102,12 +102,26 @@ def clear
102
102
#
103
103
# Use #size if you want the total number of documents.
104
104
#
105
+ # If args or block are present, #count will delegate to the
106
+ # #count method on +target+ and will include both persisted
107
+ # and non-persisted documents.
108
+ #
105
109
# @example Get the count of persisted documents.
106
110
# person.addresses.count
107
111
#
112
+ # @example Get the count of all documents matching a block.
113
+ # person.addresses.count { |a| a.country == "FR" }
114
+ #
115
+ # @example Use #persisted? inside block to count persisted documents.
116
+ # person.addresses.count { |a| a.persisted? && a.country == "FR" }
117
+ #
118
+ # @param [ Object, Array<Object> ] args Args to delegate to the target.
119
+ #
108
120
# @return [ Integer ] The total number of persisted embedded docs, as
109
121
# flagged by the #persisted? method.
110
- def count
122
+ def count ( *args , &block )
123
+ return _target . count ( *args , &block ) if args . any? || block
124
+
111
125
_target . select { |doc | doc . persisted? } . size
112
126
end
113
127
You can’t perform that action at this time.
0 commit comments