ruby-on-rails - 为什么activerecord没有?返回假?
问题描述
我有一个模型帐户has_and_belongs_to_many :categories
。
在测试给定类别是否已经链接到一个帐户时(在创建它之前),我这样做:
account = Account.find(1)
account.categories.where(name: "rent").none?
这将返回false
(尽管“租金”是一个已经存在的帐户的类别)。
account.categories.where(name: "monkey").none?
返回true
为什么现有关系不返回而不true
存在false
?或者我应该使用另一种方法来测试它吗?
解决方案
你可能想要这个any?
方法,因为......
[1,2].none?
=> false
[1,2].any?
=> true
推荐阅读
- c# - Why do I have to reference components in start, but declare variables in no void?
- c# - Best Practice to use connection string in Asp.Net Core app
- javascript - 在 React Spring + 样式化组件中传递值(插值)
- string - 函数是否有可能(或需要)返回一个 rust 的向量<&str>
- java - 如何生成包含可执行 jar 和相关脚本/资源的存档
- node.js - 使用请求流和异步处理关闭连接
- android - 在闭包中捕获时,包装到要修改的参考对象中
- javascript - Error wss://mqtt.eclipse.org when create mqtt client
- python - Huey 任务队列仅在上次重试时处理错误并检索异常回溯
- javascript - 在 1 行代码中以 Javascript 显示实时日期 + 时间