检查下面的注释。为什么在我执行 p variant_attributes
之前,blank?
方法返回错误,而在执行之后,它工作正常?
Rails 3.0.3 上的 Ruby 1.9.2-p0
p variant_attributes.blank?
# => NoMethodError Exception: undefined method `zero?' for {"Brocade w/ Grande Stripe backing"=>3}:ActiveSupport::OrderedHash
p variant_attributes
# => [#<VariantAttribute id: 1251, variant_id: 561, product_option_id: 838, value: "Brocade w/ Grande Stripe backing">]
p variant_attributes.blank?
# => false
最佳答案
如果 variant_attributes
是一种 ActiveRecord 记录集合(看起来像),那么可能是因为 rails 使用延迟加载从数据库中获取记录,但 为空?
方法不会触发实际加载。
你可能想调用 variant_attributes
上的 all
方法来手动触发加载,或者如果你不想这样做,你可以选择 variant_attributes.count.zero?
而不是 variant_attributes.blank?
参见 Pratik Naik's blog post about ActiveRecord 3.0详情查询接口(interface)
关于ruby-on-rails - 未定义方法 'zero' ... ActiveSupport::OrderedHash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846500/