Check the note below. Why is it before I do p variant_attributes
, blank?
method returned error, while after it, it works fine?
Ruby 1.9.2-p0 on Rails 3.0.3
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.bla开发者_开发问答nk?
# => false
If variant_attributes
is a kind of ActiveRecord collection of records (which it looks like) then it is probably because rails uses lazy loading to fetch records from the database but the blank?
method does not trigger the actual loading.
You may want to call the all
method on variant_attributes
to manually trigger the loading, or if you don't want to do that, you may go for variant_attributes.count.zero?
instead of variant_attributes.blank?
See Pratik Naik's blog post about ActiveRecord 3.0 query interface for the details
精彩评论