Search code examples
ruby-on-railsactiverecordarel

`.where` ARel query with enum mapped column returns exclusively entries with `0`


I have a model that has an enum for one of its fields.

class Foo < ActiveRecord::Base

   enum status: [:yes, :no, :maybe, :so]

end

Status is an integer type field. For any Foo records, I can use foo.yes!, foo.no! etc and the corresponding record returns true for foo.yes?, foo.no? as expected.

However, when I try to query for all status='no', status='maybe' or status='so' records (with the method Foo.where(status: 'no'), Foo.where(status: 'maybe'), etc.), it always returns entries that are status='yes'. I checked the raw SQL query and it seems to always only query for when status=0 or when status is yes.

Why is this so?


Solution

  • ActiveRecord does not provide such an interface for querying enums. You can query

    Foo.where(status: 0)
    

    Or

    Foo.yes