首页 > 解决方案 > 为什么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?或者我应该使用另一种方法来测试它吗?

标签: ruby-on-railsactiverecord

解决方案


你可能想要这个any?方法,因为......

[1,2].none?
=> false

[1,2].any?
=> true

推荐阅读