Q:

Query where based on the values of a field that is an array

I have a class User that has a field permissions that is an array. 
t.text  "permissions",  default: [],  array: true

The array permissions could have one or more of the strings admin, guest, and editor.
How do I write a where query to retrieve all Users which have a specific permission, say editor? I want to do this in my controller.

A:

Assuming that you're using PostgreSQL, you use any:

9.23.3. ANY/SOME (array)

expression operator ANY (array expression)
expression operator SOME (array expression)

The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result.

Using x = any(some_array) is a convenient way of checking if x is equal to any of the elements of some_array so you could say:
User.where(':permission = any(permissions)', :permission => your_permission)

You could also use the overlaps operator (&&)
User.where('permissions && array[?]', your_permission)

The array[...] syntax is a convenient way to build array literals in PostgreSQL without having to worry about quoting issues too much.

