jaycode
jaycode

Reputation: 2958

Undefined method 'zero' for ... ActiveSupport::OrderedHash

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.blank?
# => false

Upvotes: 2

Views: 2379

Answers (1)

edgerunner
edgerunner

Reputation: 14973

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

Upvotes: 7

Related Questions