enums - 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.
解决方案
Rails 的枚举让您可以访问一些类和实例方法。实例方法之一是布尔检查。因此,您可以将表达式重写为:
book.read? || book.incomplete? || book.not_started?
推荐阅读
- android - 在 Room Database Android 的 @Dao 文件中找不到 @Entity 类名
- reactjs - 如何将 React 应用程序部署到 azure 并更改默认页面
- python - 在节点js中将一个目录中的文件打开到另一个目录
- apache-spark - 过滤 RDD 以返回
- javascript - 如何在 react-tabulator 中插入新行
- gradle - dependsOn 另一个带有参数值的任务
- mysql - 无法在虚拟环境中为 django 安装 mysqlclient
- svg - 使用 svg 时,我是否需要调整 viewBox 大小的数字和启用背景中的相同数字?
- google-cloud-platform - 对外部 Google 品牌帐号的 Oauth 访问权限
- cmake - 运行 cmake std 教程第 2 步示例时出错