Question

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)
Was it helpful?

Solution

I prefer using

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

OTHER TIPS

try using any_of

Collection.any_of({ assets: my_asset }, { assets: [] })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top