首页 > 解决方案 > 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.

标签: enumsincluderuby-on-rails-5conditional-statements

解决方案


Rails 的枚举让您可以访问一些类和实例方法。实例方法之一是布尔检查。因此,您可以将表达式重写为:

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

Rails 枚举文档


推荐阅读