Using Rails 5 enum with PG array

Since this question is still without answer, this is how it can be done:

First, there is no array: true option on the enum method, just leave it out.

Second, add a custom scope to retrieve the products matching the delivery

scope :with_delivery_type, ->(*delivery_types) do
  normalized = Array(delivery_types).flatten
  where('delivery_types @> ?', "{#{normalized.join(',')}}")
end

Last but not least, I'd recommend using a string or Postgres enum type instead of integer columns. Integer columns are problematic because for one, to read it, one needs the source code of the application that wrote the record (the version at the time of insertion) and second it is unnecessarily hard to remove or replace values.


You can use array_enum gem that was created just for this usecase https://github.com/freeletics/array_enum