Search code examples
enumsincluderuby-on-rails-5conditional-statements

How do I express an include on an enum field?


I'm using Rails 5. I have a field in my model that is an enum. When I want to check if it is one of three values, I'm doing this

book.status == :read || book.status == :incomplete || book.status == :not_started

Is there a more concise way to write the above? Maybe it's already as concise as it can be.


Solution

  • Rails' enums give you access to some class and instance methods. One of the instance methods is a boolean check. So you could rewrite your expression as:

    book.read? || book.incomplete? || book.not_started?

    Rails Enum Docs