I want to make a request which returns elements from my collection if a string is in an array or if the array is empty. I tried the following:

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

But this doesn't work.

This works but not for the empty arrays:

Collection.where(assets: my_asset)
有帮助吗?

解决方案

I prefer using

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

其他提示

try using any_of

Collection.any_of({ assets: my_asset }, { assets: [] })
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top