2014-04-07 71 views
0

如果字符串在数组中,或者数组为空,我想创建一个从我的集合中返回元素的请求。我尝试了以下内容:Mongoid:数组包含元素或为空

Collection.all_of(or: [{ assets: my_asset }, { assets: [] } ]) 

但这不起作用。

这工作,但不是为空数组:

Collection.where(assets: my_asset) 

回答

1

我更喜欢使用

Collection.where(:assets.in => [[], my_asset]) 
1

尝试使用any_of

Collection.any_of({ assets: my_asset }, { assets: [] })